From patchwork Mon Jul 2 13:41:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Kagan X-Patchwork-Id: 10501417 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 7AF4960325 for ; Mon, 2 Jul 2018 13:44:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6614E2844E for ; Mon, 2 Jul 2018 13:44:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 59AFF287A5; Mon, 2 Jul 2018 13:44:13 +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=BAD_ENC_HEADER,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 B190B2844E for ; Mon, 2 Jul 2018 13:44:12 +0000 (UTC) Received: from localhost ([::1]:32863 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fZz7k-00054u-09 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 02 Jul 2018 09:44:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44033) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fZz5s-0004BY-AD for qemu-devel@nongnu.org; Mon, 02 Jul 2018 09:42:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fZz5p-00089s-4Q for qemu-devel@nongnu.org; Mon, 02 Jul 2018 09:42:16 -0400 Received: from mail-eopbgr00093.outbound.protection.outlook.com ([40.107.0.93]:20064 helo=EUR02-AM5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fZz5o-000892-NI for qemu-devel@nongnu.org; Mon, 02 Jul 2018 09:42:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qEmarnPyi4Hl4qSOZGFN248Ly/xv7vQR5RXQBVRFus8=; b=EMK40hQgYAdQ3eeBfZ1+8hqDt0SguOWoqdPkGNuI+PV3IIn0Gz4QSoI02cdH9oqZm1Zicyw6eiCVn4y3E+ajjax7ThW4xF4BwIbdkl/7pdpE5lWJ7F6SztM/kSVAjdjx+JRh1rO3Csdqxr0hox5utbPy2IkV+i9ckn8dX2ea1Ok= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=rkagan@virtuozzo.com; Received: from rkaganb.sw.ru (185.231.240.5) by HE1PR0801MB1980.eurprd08.prod.outlook.com (2603:10a6:3:4f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.906.26; Mon, 2 Jul 2018 13:42:08 +0000 From: Roman Kagan To: qemu-devel@nongnu.org Date: Mon, 2 Jul 2018 16:41:56 +0300 Message-Id: <20180702134156.13404-3-rkagan@virtuozzo.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180702134156.13404-1-rkagan@virtuozzo.com> References: <20180702134156.13404-1-rkagan@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [185.231.240.5] X-ClientProxiedBy: AM4PR07CA0003.eurprd07.prod.outlook.com (2603:10a6:205:1::16) To HE1PR0801MB1980.eurprd08.prod.outlook.com (2603:10a6:3:4f::22) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e832a609-409d-419d-68f7-08d5e0219bcd X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989117)(5600053)(711020)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7153060)(7193020); SRVR:HE1PR0801MB1980; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1980; 3:6NT01G59s72lHNkUIxG284f7EUqzLmtzs2Zj2EAs3MesnsOU481/wpO7IJpn0I3d1Fbgjv6Lr9R31rWX0U4922eAnSQgDym+9cNBPcb0TzQmDvnVjo89RoW6OY31kKra/EFzmXjX/T1F1JvdEqI6YpD+nWFdKiztdREH5ndJbVjrCpAGxNNRnuKbovjg0XF66poqltnW7rIWbRdAIt+OrxJ/Jacb31dYO/IrO753PNvfZsbaAQ2FQMlcNiAWHXdW; 25:72dGPLkUFgyfrdQBQBq0WGsM3GMNYMRj/siLXJGcO6iVX9BClXIlK9OtHvpbi+SFGgpklzFx9aIGAnD6Q8PAqICqmO+OiL2y0c7fdZFjUc174DI7BnSZZznzqQRWy+kveNSWfxoNL94yTfkifeoxBUmCqMBnxxWcQvVdX6ySL1rjg5/aoBjViSwZyCxNnbGYo18wALqlN6z519oml3ZJr3zeqxd4JQogFH7q0kif1x7J4Rsta86OofLxXasOl+K+bA8Oori1gDAaAYix2fk5GTx5bqSLouM6Ckpm6Q0kyetut82g1vJEsy6JG4OwBqlls2n5SvG776MtOCAPgiD1QA==; 31:V6ua1Spl+qruG30dU0L7EB0XxZGF2OscL8TZgwqjQCzarEFoCEtAr1qPbTZdgpZQDF2b8UtrH8arAGoa7gSuiRh05Vt8CG5NTkb6waloLpWcFvhoTfd4s98gA16EkAIMX19EyAx7EGh2ObmE6H2zg2N8kSgVY8NUr09zLvF74Pxhi6XS8/rN0feMgscpu0vpimviQ2KUlpwMcAFQ83hBaeVE6DrZOa3YNNx0vuMV0Mc= X-MS-TrafficTypeDiagnostic: HE1PR0801MB1980: X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1980; 20:QpmwrPSmyDQ3wJgtCM52BdDA0U4/lOzQiIpTr7cBB20gfuwzC8Qd1RcO1fI2+6iIrVGC/p/9Ma8/1ZfhqmWrOIDPeAzrLyMWb+CUjyD9mun1YPXKGaLriXngiTQwZ9QT44joFgFmN3gYEcz6JP+vEsMULI5jwxY1BNtDlUT19rf4FeQ+F03/8Pod0KEb7OiKFfVY1PRJ09NElLxL2Z4CkCdGeFYs3kMPhoFE4W59lpUsx70ADImzmI+gK9QSxa8EZb7+0YE/FRXjLjJh737iqD6Vy+NZ+y+GRax5VPM54HDkOHTChb/+AQ8ioxzUavPCa3wAN8O6MpV6UBgvB+nqcGk+fdHoFpTd0U30sPQTZbt5c1yHGPG5Dsb4gIAYVhganDQcv7Up8pPDNvX6qKg+eWf36tswmCR/ATaSFiHu2jkglZ4IGNc6y7sexQzqDWWq+I+zPt1a9dBvvZ5uTyGfDAMcuLKCBoSLJLXZRUT3juzuyLPQIzth81l77+6o45SV; 4:yk356bvtTvw5ix+uHpkPvBiNBzuzkP10QcJlk3YZWg//3geo/3WLk+4sXz8AU56nRsrmcJNDyO7FLCcrsSjzQXS4cXobPr1LAYkKTATxhiGMtLNRVp5DfvfqNZTWFJDGDfUVkennRhk6JWxJ1UPJ1znk8q0S9SL2xMoZFkk3trGC7vgSLMTdipmCxvNU80YAsOFPUUIELiUgItb3Mxs9iTdj+lWSziTO7VZclu6BgfqdCpxt1uHdp5VBSwdXWLeuZduU90wV/PcC8hQrqpTA6Q== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(5005006)(8121501046)(93006095)(93001095)(3002001)(3231254)(944501410)(52105095)(10201501046)(149027)(150027)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123558120)(20161123562045)(20161123564045)(6072148)(201708071742011)(7699016); SRVR:HE1PR0801MB1980; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0801MB1980; X-Forefront-PRVS: 07215D0470 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(346002)(376002)(39840400004)(366004)(396003)(136003)(199004)(189003)(25786009)(2351001)(52116002)(81156014)(81166006)(54906003)(8676002)(4326008)(51416003)(16586007)(97736004)(316002)(53416004)(8936002)(7736002)(39060400002)(69596002)(305945005)(86362001)(575784001)(105586002)(106356001)(6916009)(478600001)(2906002)(76176011)(66066001)(6666003)(47776003)(36756003)(53936002)(11346002)(6116002)(3846002)(446003)(48376002)(956004)(1076002)(476003)(386003)(50466002)(6506007)(2616005)(486006)(6486002)(5660300001)(14444005)(6512007)(26005)(68736007)(186003)(2361001)(50226002)(16526019)(7416002); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0801MB1980; H:rkaganb.sw.ru; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0801MB1980; 23:OwPkYUlFLiHqHb4Kh+oJyLWCUd1PPT0kK7f27Ee?= =?us-ascii?Q?mxk0zxJKgE7sjcd/TTntj2cnfY81En0M53kzoO3psiIJh2oMGmBwKqRF0hn8?= =?us-ascii?Q?S4+VUlhU74XtEa6GTvgMY2rqg/UKc0WJKEmYh7RiGd6mbqN1z9A2MzjCa2zT?= =?us-ascii?Q?riaKeDoL5mxWtG3wU6NQMnd1VdiND5EdcbQOYOoorgVc4NN93zrDuB17lcAd?= =?us-ascii?Q?QGJWT+YXkX9CPzdK9no6KcV11NBQdn4hiaPyTQFDNErpKHTMKKild5GFZa54?= =?us-ascii?Q?js1DOslz4V6W47m9grEwiYPSdCfaqv0X6poBCIx6tXv8DaGuf06roAVjtNQF?= =?us-ascii?Q?IWk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1wi3w6DRbcM7LB0TpYqLcIGjsIHxPlkNSmklRdp+Q==; 5:hXz3FuKiVArKJY+GIqqXzxINCfiXVrJpUotv8uF7J7Bxw3RpJWhOyRmwwhiUU86i3txBFhgYIdTL5SLTpQjNSE4z0kw8BDsWngfA2NlryUrK75m6fbalKMz8yDZSddY6fbER4hT/f4L5Y+2lxlweDgjlGvpSA7dRqj9GSZIEeUQ=; 24:PBedKY7YzfsFNWr9Xi3eaDRtyb/x1ph83xGmPvZIGlADhNURvcRvYdwwuB+3JVdXK+VCzp6hJhHi7znYhh53Pu24JV8AHphCxjJUPTpRT3g= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1980; 7:LZDIiyvRUxQuaSiyAzT2UmtG1vf8uYVGD5qKiPDFwos8CCUJqkWDO6piGQJpqp4B06q5OXFuoTqIeeg6x4Yqvh3xtTfoZJm89/EuaH/CVYJLuuG6mHdgPhVPaUv/djv1DVT0tmL1uBeed7l2s107uiExFLilNeHc2cYiqaoTYCdIP6V4PLPvn6zVJ0YcibW7IHfYCNe+NkM+g9y5YABK4+dM2Mdd+MfFuLeKL56KC/D89vye+5nQWdR68j5fDemr; 20:p/3nUZRVDlAQAsmGT3socrjpmzs1/k5mtaNeixZVmPl11bUCJS53UENdjgb5gmJNEj6gzgzpwIr5OaPiFp58zs/Ex+IQ54dDJl3TdRB5AJyWzvw+GGp0f71qtCQXe4bjr9SekFvt0MyX6teBiEMOguZabfMByKHPQHvMz9+MYRM= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jul 2018 13:42:08.1484 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e832a609-409d-419d-68f7-08d5e0219bcd X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0801MB1980 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.0.93 Subject: [Qemu-devel] [PATCH 2/2] hyperv: ensure VP index equal to QEMU cpu_index 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: Vijayabhaskar Balakrishna , Eduardo Habkost , "Michael S. Tsirkin" , Konrad Rzeszutek Wilk , Venu Busireddy , Liran Alon , Igor Mammedov , Si-Wei Liu , Paolo Bonzini , Boris Ostrovsky , Karl Heubaum , Richard Henderson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM. It has to be owned by QEMU in order to preserve it across migration. However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index. So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU. Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug. This patch also introduces accessor functions to encapsulate the mapping between a vCPU and its vp_index. Signed-off-by: Roman Kagan --- target/i386/hyperv.h | 3 +++ target/i386/kvm_i386.h | 2 ++ hw/i386/pc.c | 5 +++++ target/i386/hyperv.c | 10 +++++++++ target/i386/kvm-stub.c | 5 +++++ target/i386/kvm.c | 47 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+) diff --git a/target/i386/hyperv.h b/target/i386/hyperv.h index eaf3df34b0..00c9b454bb 100644 --- a/target/i386/hyperv.h +++ b/target/i386/hyperv.h @@ -39,4 +39,7 @@ void kvm_hv_sint_route_destroy(HvSintRoute *sint_route); int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route); +uint32_t hyperv_vp_index(X86CPU *cpu); +X86CPU *hyperv_find_vcpu(uint32_t vp_index); + #endif diff --git a/target/i386/kvm_i386.h b/target/i386/kvm_i386.h index e5df24cad1..3057ba4f7d 100644 --- a/target/i386/kvm_i386.h +++ b/target/i386/kvm_i386.h @@ -63,4 +63,6 @@ void kvm_put_apicbase(X86CPU *cpu, uint64_t value); bool kvm_enable_x2apic(void); bool kvm_has_x2apic_api(void); + +bool kvm_hv_vpindex_settable(void); #endif diff --git a/hw/i386/pc.c b/hw/i386/pc.c index f310040351..690e8b7697 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1998,6 +1998,11 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev, } cpu->thread_id = topo.smt_id; + if (cpu->hyperv_vpindex && !kvm_hv_vpindex_settable()) { + error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX"); + return; + } + cs = CPU(cpu); cs->cpu_index = idx; diff --git a/target/i386/hyperv.c b/target/i386/hyperv.c index 7cc0fbb272..3065d765ed 100644 --- a/target/i386/hyperv.c +++ b/target/i386/hyperv.c @@ -16,6 +16,16 @@ #include "hyperv.h" #include "hyperv-proto.h" +uint32_t hyperv_vp_index(X86CPU *cpu) +{ + return CPU(cpu)->cpu_index; +} + +X86CPU *hyperv_find_vcpu(uint32_t vp_index) +{ + return X86_CPU(qemu_get_cpu(vp_index)); +} + int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit) { CPUX86State *env = &cpu->env; diff --git a/target/i386/kvm-stub.c b/target/i386/kvm-stub.c index bda4dc2f0c..e7a673e5db 100644 --- a/target/i386/kvm-stub.c +++ b/target/i386/kvm-stub.c @@ -40,3 +40,8 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function, abort(); } #endif + +bool kvm_hv_vpindex_settable(void) +{ + return false; +} diff --git a/target/i386/kvm.c b/target/i386/kvm.c index 032f0ad2fc..67c3bedb4c 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -85,6 +85,7 @@ static bool has_msr_hv_hypercall; static bool has_msr_hv_crash; static bool has_msr_hv_reset; static bool has_msr_hv_vpindex; +static bool hv_vpindex_settable; static bool has_msr_hv_runtime; static bool has_msr_hv_synic; static bool has_msr_hv_stimer; @@ -162,6 +163,11 @@ bool kvm_enable_x2apic(void) has_x2apic_api); } +bool kvm_hv_vpindex_settable(void) +{ + return hv_vpindex_settable; +} + static int kvm_get_tsc(CPUState *cs) { X86CPU *cpu = X86_CPU(cs); @@ -744,6 +750,37 @@ static int hyperv_handle_properties(CPUState *cs) return 0; } +static int hyperv_init_vcpu(X86CPU *cpu) +{ + if (cpu->hyperv_vpindex && !hv_vpindex_settable) { + /* + * the kernel doesn't support setting vp_index; assert that its value + * is in sync + */ + int ret; + struct { + struct kvm_msrs info; + struct kvm_msr_entry entries[1]; + } msr_data = { + .info.nmsrs = 1, + .entries[0].index = HV_X64_MSR_VP_INDEX, + }; + + ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data); + if (ret < 0) { + return ret; + } + assert(ret == 1); + + if (msr_data.entries[0].data != hyperv_vp_index(cpu)) { + error_report("kernel's vp_index != QEMU's vp_index"); + return -ENXIO; + } + } + + return 0; +} + static Error *invtsc_mig_blocker; #define KVM_MAX_CPUID_ENTRIES 100 @@ -1147,6 +1184,11 @@ int kvm_arch_init_vcpu(CPUState *cs) has_msr_tsc_aux = false; } + r = hyperv_init_vcpu(cpu); + if (r) { + goto fail; + } + return 0; fail: @@ -1338,6 +1380,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s) has_pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2); #endif + hv_vpindex_settable = kvm_check_extension(s, KVM_CAP_HYPERV_VP_INDEX); + ret = kvm_get_supported_msrs(s); if (ret < 0) { return ret; @@ -1887,6 +1931,9 @@ static int kvm_put_msrs(X86CPU *cpu, int level) if (has_msr_hv_runtime) { kvm_msr_entry_add(cpu, HV_X64_MSR_VP_RUNTIME, env->msr_hv_runtime); } + if (cpu->hyperv_vpindex && hv_vpindex_settable) { + kvm_msr_entry_add(cpu, HV_X64_MSR_VP_INDEX, hyperv_vp_index(cpu)); + } if (cpu->hyperv_synic) { int j;