From patchwork Mon Nov 7 08:08:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Feng" X-Patchwork-Id: 9414475 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 4C84B6022E for ; Mon, 7 Nov 2016 08:42:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C57B28B04 for ; Mon, 7 Nov 2016 08:42:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3103D28C78; Mon, 7 Nov 2016 08:42:45 +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 AF61828B04 for ; Mon, 7 Nov 2016 08:42:44 +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 1c3fTm-0005YU-5K; Mon, 07 Nov 2016 08:40:34 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c3fTl-0005Wr-59 for xen-devel@lists.xen.org; Mon, 07 Nov 2016 08:40:33 +0000 Received: from [193.109.254.147] by server-4.bemta-6.messagelabs.com id E6/2F-25709-00E30285; Mon, 07 Nov 2016 08:40:32 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrHLMWRWlGSWpSXmKPExsVywNykWPe/rUK EwfMJ5hZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8ash/3sBe3yFc/WFjYwzpHsYuTkEBKokLh7 8RwbiC0hwCtxZNkMVgjbT+LI6i1AcS6gmj5Gibs/+1lAEmwCihIHLx4CKxIRkJa49vkyI0gRs 8ACRonGi0eZQRLCAqESjy59ACtiEVCVOD77CZjNK+AoseLqPKgNchIbdv9nBLE5BZwk7t2YwA pxkaPElf/zmScw8i5gZFjFqFGcWlSWWqRrZKCXVJSZnlGSm5iZo2toYKaXm1pcnJiempOYVKy XnJ+7iREYDgxAsIPx17KAQ4ySHExKoryWFgoRQnxJ+SmVGYnFGfFFpTmpxYcYZTg4lCR4b9kA 5QSLUtNTK9Iyc4CBCZOW4OBREuF9ApLmLS5IzC3OTIdInWJUlBLndQKGs5AASCKjNA+uDRYNl xhlpYR5GYEOEeIpSC3KzSxBlX/FKM7BqCTMexpkPE9mXgnc9FdAi5mAFruFyIAsLklESEk1MK 74dds60zy080Jc6Q7e/PlJOmdjtu4VW3X87rbU+PSrc9qY1SRD90cn3tsUdmLCv1nrFlj6pLm HlSROL3CYytG7r37GecGE6ItJmQXqlmZx8p3nuxsuJ/81ub1RvJNr6bkP/ZP2Msg5LzENsTpg veQAc8y8u7JuaaulbWKmyIS++iqQ+vepvhJLcUaioRZzUXEiABqq25iBAgAA X-Env-Sender: feng.wu@intel.com X-Msg-Ref: server-12.tower-27.messagelabs.com!1478508024!69839345!4 X-Originating-IP: [192.55.52.115] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.0.13; banners=-,-,- X-VirusChecked: Checked Received: (qmail 32963 invoked from network); 7 Nov 2016 08:40:31 -0000 Received: from mga14.intel.com (HELO mga14.intel.com) (192.55.52.115) by server-12.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 7 Nov 2016 08:40:31 -0000 Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP; 07 Nov 2016 00:40:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,605,1473145200"; d="scan'208";a="28337055" Received: from unknown (HELO feng-bdw-de-pi.bj.intel.com) ([10.238.154.57]) by orsmga005.jf.intel.com with ESMTP; 07 Nov 2016 00:40:29 -0800 From: Feng Wu To: xen-devel@lists.xen.org Date: Mon, 7 Nov 2016 16:08:00 +0800 Message-Id: <1478506083-14560-4-git-send-email-feng.wu@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1478506083-14560-1-git-send-email-feng.wu@intel.com> References: <1478506083-14560-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 v7 3/6] 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 7e7bc8b..d70acec 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)