From patchwork Fri Nov 18 01:57:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Feng" X-Patchwork-Id: 9435669 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 B0BAD60238 for ; Fri, 18 Nov 2016 02:32:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A5D372972B for ; Fri, 18 Nov 2016 02:32:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9ABD529733; Fri, 18 Nov 2016 02:32:51 +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 0C2DF29732 for ; Fri, 18 Nov 2016 02:32:50 +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 1c7Ywe-00045v-VD; Fri, 18 Nov 2016 02:30:28 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c7Ywe-00045M-31 for xen-devel@lists.xen.org; Fri, 18 Nov 2016 02:30:28 +0000 Received: from [85.158.137.68] by server-9.bemta-3.messagelabs.com id 6B/A9-08915-3C76E285; Fri, 18 Nov 2016 02:30:27 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrPLMWRWlGSWpSXmKPExsVywNykWPdQul6 EwY+nehZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bU/VNZC9rlK+ZO+sLcwDhHsouRk0NIoFJi wry9TCC2hACvxJFlM1gh7ACJfVM/AdlcQDV9jBK3OnYygiTYBBQlDl48BFYkIiAtce3zZUaQI maBBYwSjRePMoMkhAVCJa7vOs8GYrMIqEocu3oRLM4r4ChxqrONDWKDnMSG3f/BhnIKOEkcX3 2bBeIiR4n9Uz+wTGDkXcDIsIpRozi1qCy1SNfIWC+pKDM9oyQ3MTNH19DAWC83tbg4MT01JzG pWC85P3cTIzAg6hkYGHcw9u31O8QoycGkJMrbHq0XIcSXlJ9SmZFYnBFfVJqTWnyIUYaDQ0mC tz8NKCdYlJqeWpGWmQMMTZi0BAePkgjvjVSgNG9xQWJucWY6ROoUo6KUOG8PSJ8ASCKjNA+uD RYPlxhlpYR5GRkYGIR4ClKLcjNLUOVfMYpzMCoJ824BmcKTmVcCN/0V0GImoMV7BHRAFpckIq SkGhhZuzjX7tjZxdHGHxPaWFwax9N/+8KDUxm9PtEObFO0HC1LfrE/fal3zCC8Xklq6ekkUf1 1Hm2f84veaO+L33Q7QOxBxN9fOTuYS9yWuTY/OGN2iPfS/BceaxNCNc/vDa3+udv7pX9Twgqu 9XP7JVmsJu1S33CM86m70MKYM16b1C4rfQlZxKfEUpyRaKjFXFScCAAt2Mf2ggIAAA== X-Env-Sender: feng.wu@intel.com X-Msg-Ref: server-3.tower-31.messagelabs.com!1479436223!71575017!1 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.16; banners=-,-,- X-VirusChecked: Checked Received: (qmail 50007 invoked from network); 18 Nov 2016 02:30:25 -0000 Received: from mga14.intel.com (HELO mga14.intel.com) (192.55.52.115) by server-3.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 18 Nov 2016 02:30:25 -0000 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP; 17 Nov 2016 18:30:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.31,655,1473145200"; d="scan'208"; a="1070084856" Received: from unknown (HELO feng-bdw-de-pi.bj.intel.com) ([10.238.154.55]) by fmsmga001.fm.intel.com with ESMTP; 17 Nov 2016 18:30:20 -0800 From: Feng Wu To: xen-devel@lists.xen.org Date: Fri, 18 Nov 2016 09:57:20 +0800 Message-Id: <1479434244-10223-4-git-send-email-feng.wu@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1479434244-10223-1-git-send-email-feng.wu@intel.com> References: <1479434244-10223-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 v8 3/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 Acked-by: Kevin Tian --- 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 a8dcabe..a1f7903 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)