From patchwork Wed Jun 14 16:51:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 9786975 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 3A84960325 for ; Wed, 14 Jun 2017 16:54:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2B7BD1FF15 for ; Wed, 14 Jun 2017 16:54:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 206A928604; Wed, 14 Jun 2017 16:54:57 +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 A43EE1FF15 for ; Wed, 14 Jun 2017 16:54:56 +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 1dLBWk-0006J4-UA; Wed, 14 Jun 2017 16:52:18 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dLBWj-0006Ii-SF for xen-devel@lists.xenproject.org; Wed, 14 Jun 2017 16:52:17 +0000 Received: from [85.158.143.35] by server-11.bemta-6.messagelabs.com id DC/55-03587-1C961495; Wed, 14 Jun 2017 16:52:17 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrGLMWRWlGSWpSXmKPExsVysyfVTfdApmO kwbp2WYvvWyYzOTB6HP5whSWAMYo1My8pvyKBNWPX0cXMBb/1Kr7Nu8HewPhAuYuRi0NIYDOj xO+HjUwQznJGiTmHN7F3MXJysAnoSuy4+ZoZxBYRCJV4uuA7M0gRs8B1RonTO+azgSSEBbwlT h25A2RzcLAIqEosuSAOEuYVsJJY3T+LFcSWEJCTaDh/H2wOp4C1xNTtk5lAyoWAam6u0J/AyL 2AkWEVo0ZxalFZapGukYleUlFmekZJbmJmjq6hgZlebmpxcWJ6ak5iUrFecn7uJkagfxmAYAf jvo+RhxglOZiURHmnCjpGCvEl5adUZiQWZ8QXleakFh9ilOHgUJLgPZwBlBMsSk1PrUjLzAEG GkxagoNHSYR3aypQmre4IDG3ODMdInWKUVFKnDcQpE8AJJFRmgfXBgvuS4yyUsK8jECHCPEUp BblZpagyr9iFOdgVBLmLUsHmsKTmVcCN/0V0GImoMVBFxxAFpckIqSkGhiD5sV+5V7iV/Y61r OO5d335W8OqbJW6Cy5/b3rTwdL/O3J2hw/d5lzPA5KZN1t2yz9i1t1jmDR9L9+/Y7aG7LTgiQ lt/ctZ3U2XL6o3FKzqHxHU0jp4zqXqW+U+ovmu94W9havkr3TNOX9SbctTe+YRE9GZ0lHPjtd 8HPOwSOVXyyW7rY/mKfEUpyRaKjFXFScCADRc0ZCaQIAAA== X-Env-Sender: andre.przywara@arm.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1497459135!67715453!1 X-Originating-IP: [217.140.101.70] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.19; banners=-,-,- X-VirusChecked: Checked Received: (qmail 30089 invoked from network); 14 Jun 2017 16:52:16 -0000 Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by server-4.tower-21.messagelabs.com with SMTP; 14 Jun 2017 16:52:16 -0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 72E0215B2; Wed, 14 Jun 2017 09:52:15 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.207.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 23C293F3E1; Wed, 14 Jun 2017 09:52:14 -0700 (PDT) From: Andre Przywara To: Julien Grall , Stefano Stabellini Date: Wed, 14 Jun 2017 17:51:50 +0100 Message-Id: <20170614165223.7543-2-andre.przywara@arm.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20170614165223.7543-1-andre.przywara@arm.com> References: <20170614165223.7543-1-andre.przywara@arm.com> Cc: xen-devel@lists.xenproject.org, Vijaya Kumar K , Vijay Kilari , Shanker Donthineni , Manish Jaggi Subject: [Xen-devel] [PATCH v12 01/34] ARM: vGIC: avoid rank lock when reading priority 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 When reading the priority value of a virtual interrupt, we were taking the respective rank lock so far. However for forwarded interrupts (Dom0 only so far) this may lead to a deadlock with the following call chain: - MMIO access to change the IRQ affinity, calling the ITARGETSR handler - this handler takes the appropriate rank lock and calls vgic_store_itargetsr() - vgic_store_itargetsr() will eventually call vgic_migrate_irq() - if this IRQ is already in-flight, it will remove it from the old VCPU and inject it into the new one, by calling vgic_vcpu_inject_irq() - vgic_vcpu_inject_irq will call vgic_get_virq_priority() - vgic_get_virq_priority() tries to take the rank lock - again! It seems like this code path has never been exercised before. Fix this by avoiding taking the lock in vgic_get_virq_priority() (like we do in vgic_get_target_vcpu()). Actually we are just reading one byte, and priority changes while interrupts are handled are a benign race that can happen on real hardware too. So it is safe to just prevent the compiler from reading from the struct more than once. Signed-off-by: Andre Przywara Reviewed-by: Julien Grall --- xen/arch/arm/vgic-v2.c | 13 ++++++++----- xen/arch/arm/vgic-v3.c | 11 +++++++---- xen/arch/arm/vgic.c | 8 +------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/xen/arch/arm/vgic-v2.c b/xen/arch/arm/vgic-v2.c index dc9f95b..9fa42e1 100644 --- a/xen/arch/arm/vgic-v2.c +++ b/xen/arch/arm/vgic-v2.c @@ -252,15 +252,15 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info, case VRANGE32(GICD_IPRIORITYR, GICD_IPRIORITYRN): { uint32_t ipriorityr; + uint8_t rank_index; if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width; rank = vgic_rank_offset(v, 8, gicd_reg - GICD_IPRIORITYR, DABT_WORD); if ( rank == NULL ) goto read_as_zero; + rank_index = REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR, DABT_WORD); vgic_lock_rank(v, rank, flags); - ipriorityr = rank->ipriorityr[REG_RANK_INDEX(8, - gicd_reg - GICD_IPRIORITYR, - DABT_WORD)]; + ipriorityr = ACCESS_ONCE(rank->ipriorityr[rank_index]); vgic_unlock_rank(v, rank, flags); *r = vgic_reg32_extract(ipriorityr, info); @@ -499,7 +499,7 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info, case VRANGE32(GICD_IPRIORITYR, GICD_IPRIORITYRN): { - uint32_t *ipriorityr; + uint32_t *ipriorityr, priority; if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width; rank = vgic_rank_offset(v, 8, gicd_reg - GICD_IPRIORITYR, DABT_WORD); @@ -508,7 +508,10 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info, ipriorityr = &rank->ipriorityr[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR, DABT_WORD)]; - vgic_reg32_update(ipriorityr, r, info); + priority = ACCESS_ONCE(*ipriorityr); + vgic_reg32_update(&priority, r, info); + ACCESS_ONCE(*ipriorityr) = priority; + vgic_unlock_rank(v, rank, flags); return 1; } diff --git a/xen/arch/arm/vgic-v3.c b/xen/arch/arm/vgic-v3.c index d10757a..9018ddc 100644 --- a/xen/arch/arm/vgic-v3.c +++ b/xen/arch/arm/vgic-v3.c @@ -515,14 +515,15 @@ static int __vgic_v3_distr_common_mmio_read(const char *name, struct vcpu *v, case VRANGE32(GICD_IPRIORITYR, GICD_IPRIORITYRN): { uint32_t ipriorityr; + uint8_t rank_index; if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width; rank = vgic_rank_offset(v, 8, reg - GICD_IPRIORITYR, DABT_WORD); if ( rank == NULL ) goto read_as_zero; + rank_index = REG_RANK_INDEX(8, reg - GICD_IPRIORITYR, DABT_WORD); vgic_lock_rank(v, rank, flags); - ipriorityr = rank->ipriorityr[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR, - DABT_WORD)]; + ipriorityr = ACCESS_ONCE(rank->ipriorityr[rank_index]); vgic_unlock_rank(v, rank, flags); *r = vgic_reg32_extract(ipriorityr, info); @@ -630,7 +631,7 @@ static int __vgic_v3_distr_common_mmio_write(const char *name, struct vcpu *v, case VRANGE32(GICD_IPRIORITYR, GICD_IPRIORITYRN): { - uint32_t *ipriorityr; + uint32_t *ipriorityr, priority; if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width; rank = vgic_rank_offset(v, 8, reg - GICD_IPRIORITYR, DABT_WORD); @@ -638,7 +639,9 @@ static int __vgic_v3_distr_common_mmio_write(const char *name, struct vcpu *v, vgic_lock_rank(v, rank, flags); ipriorityr = &rank->ipriorityr[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR, DABT_WORD)]; - vgic_reg32_update(ipriorityr, r, info); + priority = ACCESS_ONCE(*ipriorityr); + vgic_reg32_update(&priority, r, info); + ACCESS_ONCE(*ipriorityr) = priority; vgic_unlock_rank(v, rank, flags); return 1; } diff --git a/xen/arch/arm/vgic.c b/xen/arch/arm/vgic.c index 83569b0..18fe420 100644 --- a/xen/arch/arm/vgic.c +++ b/xen/arch/arm/vgic.c @@ -227,14 +227,8 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq) static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq) { struct vgic_irq_rank *rank = vgic_rank_irq(v, virq); - unsigned long flags; - int priority; - - vgic_lock_rank(v, rank, flags); - priority = rank->priority[virq & INTERRUPT_RANK_MASK]; - vgic_unlock_rank(v, rank, flags); - return priority; + return ACCESS_ONCE(rank->priority[virq & INTERRUPT_RANK_MASK]); } bool vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq)