From patchwork Tue Jun 19 01:41:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 10472833 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 422F46032A for ; Tue, 19 Jun 2018 01:55:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A1D7289E5 for ; Tue, 19 Jun 2018 01:55:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1C16A28B3E; Tue, 19 Jun 2018 01:55:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8D41A289E5 for ; Tue, 19 Jun 2018 01:55:23 +0000 (UTC) Received: from localhost ([::1]:38420 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fV5re-0000at-Q8 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Jun 2018 21:55:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45010) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fV5h9-0000i3-Fq for qemu-devel@nongnu.org; Mon, 18 Jun 2018 21:44:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fV5h8-0007ux-F4 for qemu-devel@nongnu.org; Mon, 18 Jun 2018 21:44:31 -0400 Received: from mail-ot0-x242.google.com ([2607:f8b0:4003:c0f::242]:44058) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fV5h8-0007uV-8k; Mon, 18 Jun 2018 21:44:30 -0400 Received: by mail-ot0-x242.google.com with SMTP id w13-v6so20755671ote.11; Mon, 18 Jun 2018 18:44:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=Dw2InTU21LfLKSNCXgyLaAbOpeDjlXNHVGmcP9gC4vM=; b=B1Qiq7JTYP3A97G54uDtbfcYzdMJ0anmqHkoU2W1ot9TOMamvK0fnMdbM3ZxBLBn3Z q0gdjl/4W/q/jAfpWqk5SBRaK8XAOF6ftNZ7kgFLaevlGFZjlHIDgQNDuaQl8ARUmFHj Gk32xfWDFKNOyDn/cvxxbOzQSZH8JP+VmUHBSuSIpQrvaRZHXyiS+tV2kz7ah0wb5/64 0877WtyrCyi6ukwq9ro8WHNBUO5kWlywszx5DKH3JnMm2BXDI12aeKd6xa5cq4RcVVui b5R6ZVebqga2A643HSqUkwsx4zHQFR1Hj8LLnXlBA46k6NJTKS8/erqlWhG0SKj0YB+6 nwoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=Dw2InTU21LfLKSNCXgyLaAbOpeDjlXNHVGmcP9gC4vM=; b=dBRlmWsq2Cu+lU6GnE94ggrTkLkl2TRU6qFXgSnxb+mOVbBqzQuNm4p64CrLfFlGiY n0fYk/KUeVQOc4MsDwg9jEszj9TyObzhbDI9frB7/HzX7iWulTrEIka2a0rpMq2YCzj2 fdPiDMXAI6PIvzHvHv/hDB4sgCKf5RFGIK8AQF+w4q0n2kHS1E0c8heTTxtOpLiY8v6Y rEqrlaph8T8XVlYVuzNP1SBgFHdeocQiUDFObpsTyrVcZhWDPp1akIDeGJ0mUVcM4FHC FYtesBSv4kiykV3OhVp2PUR+HT6ozEJGbaZQrcUVwEICaQWgr+rH44ntWiIzNkx/Hj1H R9Ng== X-Gm-Message-State: APt69E3TbwK93avWJ73qffxVe0KewvgMCsZ98J6ShwwvEVE+SVQ4gqN5 sfhl48qEb97gXiJqFILRDTmZF/YF X-Google-Smtp-Source: ADUXVKIb2L0Txr8n1T39WW41ivHfduOxgBmrMKDo27zTeMVyerWGcdr4DSa/iqeYGDpgul+yvENk3g== X-Received: by 2002:a9d:3684:: with SMTP id h4-v6mr9805418otc.186.1529372669160; Mon, 18 Jun 2018 18:44:29 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id w14-v6sm7383666otj.31.2018.06.18.18.44.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Jun 2018 18:44:28 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 18 Jun 2018 20:41:39 -0500 Message-Id: <20180619014319.28272-14-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180619014319.28272-1-mdroth@linux.vnet.ibm.com> References: <20180619014319.28272-1-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4003:c0f::242 Subject: [Qemu-devel] [PATCH 013/113] 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: David Gibson , qemu-stable@nongnu.org, Greg Kurz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 != 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 (cherry picked from commit 5d0fb1508e2d279da74ef4a103e8def9b52c6304) Signed-off-by: Greg Kurz --- 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 0be60fb044..a36494b87b 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -100,6 +100,21 @@ #define PHANDLE_XICP 0x00001111 +/* 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 == 0; +} + static ICSState *spapr_ics_create(sPAPRMachineState *spapr, const char *type_ics, int nr_irqs, Error **errp) @@ -346,7 +361,7 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr) int index = spapr_get_vcpu_id(cpu); int compat_smt = MIN(smp_threads, ppc_compat_max_vthreads(cpu)); - if (index % spapr->vsmt != 0) { + if (!spapr_is_thread0_in_vcore(spapr, cpu)) { continue; } @@ -630,7 +645,7 @@ static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr) DeviceClass *dc = DEVICE_GET_CLASS(cs); int offset; - if (index % spapr->vsmt != 0) { + if (!spapr_is_thread0_in_vcore(spapr, cpu)) { continue; } @@ -2230,7 +2245,7 @@ static void spapr_init_cpus(sPAPRMachineState *spapr) 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)); } if (i < boot_cores_nr) { @@ -3274,7 +3289,8 @@ void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev, return; } - drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * spapr->vsmt); + drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, + spapr_vcpu_id(spapr, cc->core_id)); g_assert(drc); spapr_drc_detach(drc); @@ -3303,7 +3319,8 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev, cc->core_id); return; } - drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * spapr->vsmt); + drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, + spapr_vcpu_id(spapr, cc->core_id)); g_assert(drc || !mc->has_hotpluggable_cpus); @@ -3652,8 +3669,7 @@ void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index, Error **errp) sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); int vcpu_id; - vcpu_id = - (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads; + vcpu_id = spapr_vcpu_id(spapr, cpu_index); if (kvm_enabled() && !kvm_vcpu_id_is_valid(vcpu_id)) { error_setg(errp, "Can't create CPU with id %d in KVM", vcpu_id);