From patchwork Thu Jul 7 21:49:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Feiner X-Patchwork-Id: 9219667 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 7926960467 for ; Thu, 7 Jul 2016 21:50:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CBE927F8F for ; Thu, 7 Jul 2016 21:50:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 616E528066; Thu, 7 Jul 2016 21:50:28 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD93E27F8F for ; Thu, 7 Jul 2016 21:50:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932109AbcGGVuS (ORCPT ); Thu, 7 Jul 2016 17:50:18 -0400 Received: from mail-pf0-f179.google.com ([209.85.192.179]:33633 "EHLO mail-pf0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752230AbcGGVuP (ORCPT ); Thu, 7 Jul 2016 17:50:15 -0400 Received: by mail-pf0-f179.google.com with SMTP id i123so9504078pfg.0 for ; Thu, 07 Jul 2016 14:50:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=4yefoug4zMFIZjRGoWJjV43laHjIx7i0NdOhbsUHGew=; b=JzahBGL71d6agfMAtY3KqcXNANpp2GE4AYnxz6Z/qNn0zg2laP2M2Lgn2PRRnMzP/D /+ZTwyWJq12b6pda5GkNvRFEDyGFaPPPaSKCIcFBazhLxGtGNSAEeA84Vk/Psfh9gKx6 NVJTjfJuC2Z5KK+MMu0iRpgt4WdptQP04aCpcixISLl0G6H6KAn+bYS+/KHiQrFdjjMV Y2+yeAk5cq/BiEON9L9nUiE2j5x2TbkvTUHMhPMPUUk2KY0ca2FpTHEwB3WlOPmTPYZR nL7mtLhapk/zNUzdBLWT5s30+5cH/dngMl/c5senmhrPFSzIEltCX5JEcrVXB0yHPvcT d3pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=4yefoug4zMFIZjRGoWJjV43laHjIx7i0NdOhbsUHGew=; b=euNFOmSaphOTK4NnLSByBJRsFukoHwT1gBRsexPgePjB/2zumwdxsxEj6FI8sXehea /kogBImm/j+lNI90oRwy0xUmxb0KLK+cFqqGPddSjS92jnq9gujKAhOOFPYQjMwj7DgO m21VF72K59tq6Sd4XIUfPKGWrmvrZa5LkfapuVIkJHHBb5AgynX5Oiw/6W/t0oKQLIfM FLwDzyH08b36L9ikxX+7Gn7ne2/j9wFDT9Ebgh8WSVV6aeU6abMx3MwjD90jkLIiPz9P 6gxIfC7mojJRQNxlbP9d9fuSkcbLZm6p8/TIBx5TYWg7Du/u0jM1E0MLOx6CpBG3DsYO Mphg== X-Gm-Message-State: ALyK8tJ0eleR1WeS7nhn/3Lt7zam3ROq7fRJ3j1AzVBroqEwI0oZq/MCE38HJJDDqh/0KRrm X-Received: by 10.98.83.68 with SMTP id h65mr3953144pfb.112.1467928214363; Thu, 07 Jul 2016 14:50:14 -0700 (PDT) Received: from localhost ([2620:0:1009:11:8488:fd7c:5461:e570]) by smtp.gmail.com with ESMTPSA id x10sm6431335pfd.8.2016.07.07.14.50.13 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 07 Jul 2016 14:50:13 -0700 (PDT) From: Peter Feiner To: bsd@redhat.com, kai.huang@linux.intel.com, guangrong.xiao@linux.intel.com, pbonzini@redhat.com Cc: dmatlack@google.com, jmattson@google.com, kvm@vger.kernel.org, Peter Feiner Subject: [PATCH v2] kvm: ensure VMCS is current while enabling PML Date: Thu, 7 Jul 2016 14:49:58 -0700 Message-Id: X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <5409fdb558e5cc0b5967bbdb831ea28a28107cc5.1467922428.git.pfeiner@google.com> References: <5409fdb558e5cc0b5967bbdb831ea28a28107cc5.1467922428.git.pfeiner@google.com> In-Reply-To: <5409fdb558e5cc0b5967bbdb831ea28a28107cc5.1467922428.git.pfeiner@google.com> References: <5409fdb558e5cc0b5967bbdb831ea28a28107cc5.1467922428.git.pfeiner@google.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Between loading the new VMCS and enabling PML, the CPU was unpinned. If the vCPU thread were migrated to another CPU in the interim (e.g., due to preemption or sleeping alloc_page), then the VMWRITEs to enable PML would target the wrong VMCS -- or no VMCS at all: [ 2087.266950] vmwrite error: reg 200e value 3fe1d52000 (err -506126336) [ 2087.267062] vmwrite error: reg 812 value 1ff (err 511) [ 2087.267125] vmwrite error: reg 401e value 12229c00 (err 304258048) This patch ensures that the VMCS remains current while enabling PML by doing the VMWRITEs while the CPU is pinned. Allocation of the PML buffer is hoisted out of the critical section. Signed-off-by: Peter Feiner --- v1: Rearranged error handling paths and got rid of stray whitespace change. arch/x86/kvm/vmx.c | 56 +++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 64a79f2..e34965b 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -4979,6 +4979,12 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx) if (vmx_xsaves_supported()) vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); + if (enable_pml) { + ASSERT(vmx->pml_pg); + vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); + vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); + } + return 0; } @@ -7937,22 +7943,6 @@ static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) *info2 = vmcs_read32(VM_EXIT_INTR_INFO); } -static int vmx_create_pml_buffer(struct vcpu_vmx *vmx) -{ - struct page *pml_pg; - - pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); - if (!pml_pg) - return -ENOMEM; - - vmx->pml_pg = pml_pg; - - vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); - vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); - - return 0; -} - static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) { if (vmx->pml_pg) { @@ -8885,14 +8875,26 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) if (err) goto free_vcpu; + err = -ENOMEM; + + /* + * If PML is turned on, failure on enabling PML just results in failure + * of creating the vcpu, therefore we can simplify PML logic (by + * avoiding dealing with cases, such as enabling PML partially on vcpus + * for the guest, etc. + */ + if (enable_pml) { + vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!vmx->pml_pg) + goto uninit_vcpu; + } + vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) > PAGE_SIZE); - err = -ENOMEM; - if (!vmx->guest_msrs) { - goto uninit_vcpu; - } + if (!vmx->guest_msrs) + goto free_pml; vmx->loaded_vmcs = &vmx->vmcs01; vmx->loaded_vmcs->vmcs = alloc_vmcs(); @@ -8936,18 +8938,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) vmx->nested.current_vmptr = -1ull; vmx->nested.current_vmcs12 = NULL; - /* - * If PML is turned on, failure on enabling PML just results in failure - * of creating the vcpu, therefore we can simplify PML logic (by - * avoiding dealing with cases, such as enabling PML partially on vcpus - * for the guest, etc. - */ - if (enable_pml) { - err = vmx_create_pml_buffer(vmx); - if (err) - goto free_vmcs; - } - return &vmx->vcpu; free_vmcs: @@ -8955,6 +8945,8 @@ free_vmcs: free_loaded_vmcs(vmx->loaded_vmcs); free_msrs: kfree(vmx->guest_msrs); +free_pml: + vmx_destroy_pml_buffer(vmx); uninit_vcpu: kvm_vcpu_uninit(&vmx->vcpu); free_vcpu: