From patchwork Fri Oct 28 02:37:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Feng" X-Patchwork-Id: 9400999 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 96A45605EE for ; Fri, 28 Oct 2016 03:12:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 855ED2A4DE for ; Fri, 28 Oct 2016 03:12:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7A3582A4E0; Fri, 28 Oct 2016 03:12:16 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D073B2A4DE for ; Fri, 28 Oct 2016 03:12:15 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bzxXp-0006Li-0m; Fri, 28 Oct 2016 03:09:25 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bzxXn-0006LZ-Sv for xen-devel@lists.xen.org; Fri, 28 Oct 2016 03:09:24 +0000 Received: from [85.158.139.211] by server-12.bemta-5.messagelabs.com id 1E/E6-27365-361C2185; Fri, 28 Oct 2016 03:09:23 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrHLMWRWlGSWpSXmKPExsVywNwkQjfpoFC EwaVvyhZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa0b/qh3MBbfkKo4uOsTewPhZoouRk0NIoFLi 1O/rTCC2hACvxJFlM1gh7ACJnaunMnYxcgHV9DFKbPr3mB0kwSagKHHw4iGwIhEBaYlrny+DF TELLGCUaLx4lBkkISwQKnHsyn+wBhYBVYmmhtVgG3gFHCSWrW5mgdggJ7Fh939GEJtTwFHi56 E7jBAXOUjM2fGKdQIj7wJGhlWM6sWpRWWpRboWeklFmekZJbmJmTm6hgamermpxcWJ6ak5iUn Fesn5uZsYgeHAAAQ7GA82Ox9ilORgUhLlvTBZKEKILyk/pTIjsTgjvqg0J7X4EKMMB4eSBG/U AaCcYFFqempFWmYOMDBh0hIcPEoivNv3AaV5iwsSc4sz0yFSpxgVpcR5NUD6BEASGaV5cG2wa LjEKCslzMsIdIgQT0FqUW5mCar8K0ZxDkYlYV5TkCk8mXklcNNfAS1mAlo8PV0AZHFJIkJKqo Ex5Mf8z198la16+gMivvv6brDg/Lqr+kSA/EIX24kXNcz26Sz98XzF+vzMc6VBjusfLTxy4NB sc73Jz+by1cWs7k6ymP+zwTWnquyw2868jXO/pvzidoy24qy9+MkqIOb3z3nn7Tx8t928OU9j VlLXsxlGP2bLstppNdxzudbcb3Nw+wKfuK4PSizFGYmGWsxFxYkA8BtcooECAAA= X-Env-Sender: feng.wu@intel.com X-Msg-Ref: server-3.tower-206.messagelabs.com!1477624160!63641240!1 X-Originating-IP: [192.55.52.88] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTkyLjU1LjUyLjg4ID0+IDM3NDcyNQ==\n X-StarScan-Received: X-StarScan-Version: 9.0.13; banners=-,-,- X-VirusChecked: Checked Received: (qmail 21623 invoked from network); 28 Oct 2016 03:09:22 -0000 Received: from mga01.intel.com (HELO mga01.intel.com) (192.55.52.88) by server-3.tower-206.messagelabs.com with DHE-RSA-CAMELLIA256-SHA encrypted SMTP; 28 Oct 2016 03:09:22 -0000 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP; 27 Oct 2016 20:09:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.31,407,1473145200"; d="scan'208"; a="1077010841" Received: from unknown (HELO feng-bdw-de-pi.bj.intel.com) ([10.238.154.53]) by fmsmga002.fm.intel.com with ESMTP; 27 Oct 2016 20:09:17 -0700 From: Feng Wu To: xen-devel@lists.xen.org Date: Fri, 28 Oct 2016 10:37:36 +0800 Message-Id: <1477622259-3476-5-git-send-email-feng.wu@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1477622259-3476-1-git-send-email-feng.wu@intel.com> References: <1477622259-3476-1-git-send-email-feng.wu@intel.com> Cc: kevin.tian@intel.com, Feng Wu , george.dunlap@eu.citrix.com, andrew.cooper3@citrix.com, dario.faggioli@citrix.com, jbeulich@suse.com Subject: [Xen-devel] [PATCH v6 4/7] VMX: Make sure PI is in proper state before install the hooks X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP We may hit the last ASSERT() in vmx_vcpu_block in the current code, since vmx_vcpu_block() may get called before vmx_pi_switch_to() has been installed or executed. Here We use cmpxchg to update the NDST field, this can make sure we only update the NDST when vmx_pi_switch_to() has not been called. So the NDST is in a proper state in vmx_vcpu_block(). Suggested-by: Jan Beulich Signed-off-by: Feng Wu Reviewed-by: Jan Beulich --- v6: - Comments changes - Define macro 'APIC_INVALID_DEST' for '0xffffffff' v5: - Use 0xffffffff as the invalid value for NDST field. v4: - This patch is previously called "Pause/Unpause the domain before/after assigning PI hooks" - Remove the pause/unpause method - Use cmpxchg to update NDST xen/arch/x86/hvm/vmx/vmcs.c | 13 +++++-------- xen/arch/x86/hvm/vmx/vmx.c | 27 ++++++++++++++++++++++++++- xen/include/asm-x86/hvm/vmx/vmx.h | 2 ++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 1bd875a..e8e3616 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -956,16 +956,13 @@ void virtual_vmcs_vmwrite(const struct vcpu *v, u32 vmcs_encoding, u64 val) */ static void pi_desc_init(struct vcpu *v) { - uint32_t dest; - v->arch.hvm_vmx.pi_desc.nv = posted_intr_vector; - dest = cpu_physical_id(v->processor); - - if ( x2apic_enabled ) - v->arch.hvm_vmx.pi_desc.ndst = dest; - else - v->arch.hvm_vmx.pi_desc.ndst = MASK_INSR(dest, PI_xAPIC_NDST_MASK); + /* + * Mark NDST as invalid, then we can use this invalid value as a + * marker to whether update NDST or not in vmx_pi_hooks_assign(). + */ + v->arch.hvm_vmx.pi_desc.ndst = APIC_INVALID_DEST; } static int construct_vmcs(struct vcpu *v) diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index eb4770c..58694ce 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -206,14 +206,39 @@ static void vmx_pi_do_resume(struct vcpu *v) /* This function is called when pcidevs_lock is held */ void vmx_pi_hooks_assign(struct domain *d) { + struct vcpu *v; + if ( !iommu_intpost || !has_hvm_container_domain(d) ) return; ASSERT(!d->arch.hvm_domain.vmx.vcpu_block); - d->arch.hvm_domain.vmx.vcpu_block = vmx_vcpu_block; + /* + * We carefully handle the timing here: + * - Install the context switch first + * - Then set the NDST field + * - Install the block and resume hooks in the end + * + * This can make sure the PI (especially the NDST feild) is + * in proper state when we call vmx_vcpu_block(). + */ d->arch.hvm_domain.vmx.pi_switch_from = vmx_pi_switch_from; d->arch.hvm_domain.vmx.pi_switch_to = vmx_pi_switch_to; + + for_each_vcpu ( d, v ) + { + unsigned int dest = cpu_physical_id(v->processor); + struct pi_desc *pi_desc = &v->arch.hvm_vmx.pi_desc; + + /* + * We don't need to update NDST if vmx_pi_switch_to() + * has already got called. + */ + (void)cmpxchg(&pi_desc->ndst, APIC_INVALID_DEST, + x2apic_enabled ? dest : MASK_INSR(dest, PI_xAPIC_NDST_MASK)); + } + + d->arch.hvm_domain.vmx.vcpu_block = vmx_vcpu_block; d->arch.hvm_domain.vmx.pi_do_resume = vmx_pi_do_resume; } diff --git a/xen/include/asm-x86/hvm/vmx/vmx.h b/xen/include/asm-x86/hvm/vmx/vmx.h index 4cdd9b1..2f0435c 100644 --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -573,6 +573,8 @@ void vmx_pi_per_cpu_init(unsigned int cpu); void vmx_pi_hooks_assign(struct domain *d); void vmx_pi_hooks_deassign(struct domain *d); +#define APIC_INVALID_DEST 0xffffffff + /* EPT violation qualifications definitions */ #define _EPT_READ_VIOLATION 0 #define EPT_READ_VIOLATION (1UL<<_EPT_READ_VIOLATION)