From patchwork Thu Jan 12 14:58:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 9513357 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 3744660476 for ; Thu, 12 Jan 2017 15:01:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A1FB286E3 for ; Thu, 12 Jan 2017 15:01:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EE5C286E4; Thu, 12 Jan 2017 15:01:35 +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 909E8286EE for ; Thu, 12 Jan 2017 15:01:20 +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 1cRgq4-0005WO-JD; Thu, 12 Jan 2017 14:58:52 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cRgq3-0005Uo-CX for xen-devel@lists.xenproject.org; Thu, 12 Jan 2017 14:58:51 +0000 Received: from [85.158.139.211] by server-7.bemta-5.messagelabs.com id 28/06-29113-AA997785; Thu, 12 Jan 2017 14:58:50 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeJIrShJLcpLzFFi42JxWrohUnfVzPI Ig2tbRCy+b5nM5MDocfjDFZYAxijWzLyk/IoE1ow5XxoZC5okKn4vWMzWwHhTuIuRk0NCwF/i 4r9LbCA2m4COxNSnl1i7GDk4RARUJG7vNQAxmQXKJTom1IJUCANVb264xAxiswioShye2QRm8 wq4STxZcYYFYqKcxPnjP8HinALuEv/m7GYHsYWAak4t/QBlq0isnzqLDaJXUOLkzCdgvcwCEh IHX7xgnsDIOwtJahaS1AJGplWMGsWpRWWpRbqGBnpJRZnpGSW5iZk5QJ6pXm5qcXFiempOYlK xXnJ+7iZGYOAwAMEOxjVTnQ8xSnIwKYnyrvIojxDiS8pPqcxILM6ILyrNSS0+xCjDwaEkwTtr BlBOsCg1PbUiLTMHGMIwaQkOHiUR3o0gad7igsTc4sx0iNQpRkUpcd4lIAkBkERGaR5cGyxuL jHKSgnzMgIdIsRTkFqUm1mCKv+KUZyDUUkYYjxPZl4J3PRXQIuZgBZftAFbXJKIkJJqYOy8IV kUzG9jm/rsW8XjF5+/7BbUO/lxP2u1xBTH9xOUFmRfM3xuoDj9smHC3e78y5tnOycuO3QvMvB uSU/I91crDN3PzfTmEdtxf8fM35vCZ852tiyerMLC2jb9pNuTNm7W6u/PDae4xrPPvsyx/dBC 28UGliftFm1dXNM+v8c6nnlxQErN5QdKLMUZiYZazEXFiQA8Q08PlgIAAA== X-Env-Sender: prvs=1786f58dd=Paul.Durrant@citrix.com X-Msg-Ref: server-15.tower-206.messagelabs.com!1484233127!65615186!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.1.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 31945 invoked from network); 12 Jan 2017 14:58:50 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-15.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 12 Jan 2017 14:58:50 -0000 X-IronPort-AV: E=Sophos;i="5.33,349,1477958400"; d="scan'208";a="399471541" From: Paul Durrant To: Date: Thu, 12 Jan 2017 14:58:40 +0000 Message-ID: <1484233120-2015-9-git-send-email-paul.durrant@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1484233120-2015-1-git-send-email-paul.durrant@citrix.com> References: <1484233120-2015-1-git-send-email-paul.durrant@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Paul Durrant , Jan Beulich Subject: [Xen-devel] [PATCH v3 8/8] x86/hvm: serialize trap injecting producer and consumer 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: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Since injection works on a remote vCPU, and since there's no enforcement of the subject vCPU being paused, there's a potential race between the producing and consuming sides. Fix this by leveraging the vector field as synchronization variable. Signed-off-by: Jan Beulich [re-based] Signed-off-by: Paul Durrant Reviewed-by: Andrew Cooper --- Cc: Andrew Cooper v3: - Re-re-re-based after more changes. v2: - Re-re-based after Andrew's recent changes. --- xen/arch/x86/hvm/dm.c | 5 ++++- xen/arch/x86/hvm/hvm.c | 8 +++++--- xen/include/asm-x86/hvm/hvm.h | 3 +++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c index 0a7e50a..e09c8e7 100644 --- a/xen/arch/x86/hvm/dm.c +++ b/xen/arch/x86/hvm/dm.c @@ -286,13 +286,16 @@ static int inject_trap(struct domain *d, unsigned int vcpuid, if ( vcpuid >= d->max_vcpus || !(v = d->vcpu[vcpuid]) ) return -EINVAL; - if ( v->arch.hvm_vcpu.inject_trap.vector != -1 ) + if ( cmpxchg(&v->arch.hvm_vcpu.inject_trap.vector, + HVM_TRAP_VECTOR_UNSET, HVM_TRAP_VECTOR_UPDATING) != + HVM_TRAP_VECTOR_UNSET ) return -EBUSY; v->arch.hvm_vcpu.inject_trap.type = type; v->arch.hvm_vcpu.inject_trap.insn_len = insn_len; v->arch.hvm_vcpu.inject_trap.error_code = error_code; v->arch.hvm_vcpu.inject_trap.cr2 = cr2; + smp_wmb(); v->arch.hvm_vcpu.inject_trap.vector = vector; return 0; diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 8d42adc..441111b 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -539,12 +539,14 @@ void hvm_do_resume(struct vcpu *v) } /* Inject pending hw/sw trap */ - if ( v->arch.hvm_vcpu.inject_trap.vector != -1 ) + if ( v->arch.hvm_vcpu.inject_trap.vector >= 0 ) { + smp_rmb(); + if ( !hvm_event_pending(v) ) hvm_inject_event(&v->arch.hvm_vcpu.inject_trap); - v->arch.hvm_vcpu.inject_trap.vector = -1; + v->arch.hvm_vcpu.inject_trap.vector = HVM_TRAP_VECTOR_UNSET; } if ( unlikely(v->arch.vm_event) && v->arch.monitor.next_interrupt_enabled ) @@ -1563,7 +1565,7 @@ int hvm_vcpu_initialise(struct vcpu *v) (void(*)(unsigned long))hvm_assert_evtchn_irq, (unsigned long)v); - v->arch.hvm_vcpu.inject_trap.vector = -1; + v->arch.hvm_vcpu.inject_trap.vector = HVM_TRAP_VECTOR_UNSET; if ( is_pvh_domain(d) ) { diff --git a/xen/include/asm-x86/hvm/hvm.h b/xen/include/asm-x86/hvm/hvm.h index 8c95c08..bcacee3 100644 --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -77,6 +77,9 @@ enum hvm_intblk { #define HVM_HAP_SUPERPAGE_2MB 0x00000001 #define HVM_HAP_SUPERPAGE_1GB 0x00000002 +#define HVM_TRAP_VECTOR_UNSET (-1) +#define HVM_TRAP_VECTOR_UPDATING (-2) + /* * The hardware virtual machine (HVM) interface abstracts away from the * x86/x86_64 CPU virtualization assist specifics. Currently this interface