From patchwork Mon Jun 20 12:58:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 9187365 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 DE5DE6075F for ; Mon, 20 Jun 2016 13:01:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C84F8271FD for ; Mon, 20 Jun 2016 13:01:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BCC3227C2C; Mon, 20 Jun 2016 13:01:55 +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 3115B271FD for ; Mon, 20 Jun 2016 13:01:55 +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 1bEymi-0007Gf-5e; Mon, 20 Jun 2016 12:58:36 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bEymg-0007GH-LJ for xen-devel@lists.xenproject.org; Mon, 20 Jun 2016 12:58:34 +0000 Received: from [85.158.139.211] by server-1.bemta-5.messagelabs.com id 5A/FB-26908-978E7675; Mon, 20 Jun 2016 12:58:33 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrCIsWRWlGSWpSXmKPExsXS6fjDS7fyRXq 4wdsuDovvWyYzOTB6HP5whSWAMYo1My8pvyKBNePujvSC7doV7ecWsDYwnlfuYuTkEBLIk3h/ 7B4biM0rYCdx5tEpMFtCwFBi3/xVQDYHB4uAqsSfPxIgYTYBdYm2Z9tZQcIiAgYS544mgZjMA voS29axgFQIC/hLvL98lxliuJ3Em1sfwGxOAXuJlZM3gHXyCghK/N0hDBJmBiq59OE+6wRGnl kImVlIMhC2lsTDX7dYIGxtiWULXzPPAtsrLbH8HweE6SJxZyYjqgoQ219idstPxgWMHKsY1Yt Ti8pSi3TN9ZKKMtMzSnITM3N0DQ1M9XJTi4sT01NzEpOK9ZLzczcxAkOUAQh2MB6b7HyIUZKD SUmU16Y4PVyILyk/pTIjsTgjvqg0J7X4EKMGB4fAtl2rLzBKseTl56UqSfBOfARUJ1iUmp5ak ZaZA4wimFIJDh4lEd5ckDRvcUFibnFmOkTqFKOilDhvGEhCACSRUZoH1waL3EuMslLCvIxARw nxFKQW5WaWoMq/YhTnYFQS5l0NMoUnM68EbvoroMVMQIuX9YMtLklESEk1MC7zeHlOZbmDzIy ti+0umYaZmHgdZVRgC/1QocndPevcJLanusc+OgrELlQJeHyLe4KwzDrxp34lFQ67DvBlCd1d +Va7d7H8eRm1BMtbhpI/F+17wttQY51xc872n48vx/BeEl2rUR7xT/slZ/7F5XaxBr0mH83Z0 jKuf9uyv+OYw8HAt66Bj5VYijMSDbWYi4oTAVLu6EXXAgAA X-Env-Sender: JBeulich@suse.com X-Msg-Ref: server-5.tower-206.messagelabs.com!1466427510!45774857!1 X-Originating-IP: [137.65.248.74] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 31589 invoked from network); 20 Jun 2016 12:58:32 -0000 Received: from prv-mh.provo.novell.com (HELO prv-mh.provo.novell.com) (137.65.248.74) by server-5.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 20 Jun 2016 12:58:32 -0000 Received: from INET-PRV-MTA by prv-mh.provo.novell.com with Novell_GroupWise; Mon, 20 Jun 2016 06:58:30 -0600 Message-Id: <5768049302000078000F6C06@prv-mh.provo.novell.com> X-Mailer: Novell GroupWise Internet Agent 14.2.0 Date: Mon, 20 Jun 2016 06:58:27 -0600 From: "Jan Beulich" To: "xen-devel" References: <5768002C02000078000F6B8D@prv-mh.provo.novell.com> In-Reply-To: <5768002C02000078000F6B8D@prv-mh.provo.novell.com> Mime-Version: 1.0 Cc: Andrew Cooper Subject: [Xen-devel] [PATCH 11/11] 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 prodcing and consuming sides. Fix this by leveraging the vector field as synchronization variable. Signed-off-by: Jan Beulich x86/HVM: serialize trap injecting producer and consumer 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 prodcing and consuming sides. Fix this by leveraging the vector field as synchronization variable. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/control.c +++ b/xen/arch/x86/hvm/control.c @@ -106,14 +106,16 @@ static int inject_trap(struct domain *d, if ( op->vcpuid >= d->max_vcpus || (v = d->vcpu[op->vcpuid]) == NULL ) return -ENOENT; - 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.vector = op->vector; v->arch.hvm_vcpu.inject_trap.type = op->type; v->arch.hvm_vcpu.inject_trap.error_code = op->error_code; v->arch.hvm_vcpu.inject_trap.insn_len = op->insn_len; v->arch.hvm_vcpu.inject_trap.cr2 = op->cr2; + smp_wmb(); + v->arch.hvm_vcpu.inject_trap.vector = op->vector; return 0; } --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -510,10 +510,11 @@ 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(); hvm_inject_trap(&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; } } @@ -1508,7 +1509,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) ) { --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -78,6 +78,8 @@ enum hvm_intblk { #define HVM_HAP_SUPERPAGE_1GB 0x00000002 struct hvm_trap { +#define HVM_TRAP_VECTOR_UNSET (-1) +#define HVM_TRAP_VECTOR_UPDATING (-2) int16_t vector; uint8_t type; /* X86_EVENTTYPE_* */ uint8_t insn_len; /* Instruction length */ Reviewed-by: Andrew Cooper --- a/xen/arch/x86/hvm/control.c +++ b/xen/arch/x86/hvm/control.c @@ -106,14 +106,16 @@ static int inject_trap(struct domain *d, if ( op->vcpuid >= d->max_vcpus || (v = d->vcpu[op->vcpuid]) == NULL ) return -ENOENT; - 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.vector = op->vector; v->arch.hvm_vcpu.inject_trap.type = op->type; v->arch.hvm_vcpu.inject_trap.error_code = op->error_code; v->arch.hvm_vcpu.inject_trap.insn_len = op->insn_len; v->arch.hvm_vcpu.inject_trap.cr2 = op->cr2; + smp_wmb(); + v->arch.hvm_vcpu.inject_trap.vector = op->vector; return 0; } --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -510,10 +510,11 @@ 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(); hvm_inject_trap(&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; } } @@ -1508,7 +1509,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) ) { --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -78,6 +78,8 @@ enum hvm_intblk { #define HVM_HAP_SUPERPAGE_1GB 0x00000002 struct hvm_trap { +#define HVM_TRAP_VECTOR_UNSET (-1) +#define HVM_TRAP_VECTOR_UPDATING (-2) int16_t vector; uint8_t type; /* X86_EVENTTYPE_* */ uint8_t insn_len; /* Instruction length */