From patchwork Tue Jan 3 23:29:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 9495909 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 E5B9A60413 for ; Tue, 3 Jan 2017 23:31:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D664427317 for ; Tue, 3 Jan 2017 23:31:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C8B3827D13; Tue, 3 Jan 2017 23:31:38 +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 C069B27317 for ; Tue, 3 Jan 2017 23:31:37 +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 1cOYWQ-0005q8-Ea; Tue, 03 Jan 2017 23:29:38 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cOYWP-0005q1-H0 for xen-devel@lists.xenproject.org; Tue, 03 Jan 2017 23:29:37 +0000 Received: from [85.158.139.211] by server-6.bemta-5.messagelabs.com id B3/B8-15802-FD33C685; Tue, 03 Jan 2017 23:29:35 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrALMWRWlGSWpSXmKPExsVybKJsh+5945w Ig99bBC2+b5nM5MDocfjDFZYAxijWzLyk/IoE1owbCy6wFdyQrNh5r6qBsU2ki5GLQ0hgKqPE ke2HmboYOYGcg0wSZ1qKQWw2AUOJv082sYHYIgISEpsaVoDVMAtYS9x784MdxBYWsJWYtW0PC 4jNIqAq8eLFPdYuRg4OXgE3iePLdUHCEgJyEiePTWaFsDMk5vXMgbK9JBbduARlq0lcPbeJeQ IjzwJGhlWMGsWpRWWpRbqGRnpJRZnpGSW5iZk5uoYGpnq5qcXFiempOYlJxXrJ+bmbGIE+ZwC CHYx9s5wPMUpyMCmJ8haI5EQI8SXlp1RmJBZnxBeV5qQWH2KU4eBQkuDNMgLKCRalpqdWpGXm AIMPJi3BwaMkwvvBECjNW1yQmFucmQ6ROsWoKCXO+x+kTwAkkVGaB9cGC/hLjLJSwryMQIcI8 RSkFuVmlqDKv2IU52BUEuY1ApnCk5lXAjf9FdBiJqDF2wOyQRaXJCKkpBoYJfXuvrm0Ieby7c MnvOZGt5xKkTomOmP3iWk/1n7q2+g898qKORNZjz3gf2Ji1d6/tkAvvMeOrbe57mIXN/PbhV3 68/8qil3/NGf3nIOfDV/5vuLysOvwSSo7Mv/N1XIHtgPH1b9e1vt8/p0Ah2hcjfazEvOXHWfX uod7vbj0NDAgy5H5xeyDjUosxRmJhlrMRcWJADio+kBzAgAA X-Env-Sender: sstabellini@kernel.org X-Msg-Ref: server-6.tower-206.messagelabs.com!1483486172!78007438!1 X-Originating-IP: [198.145.29.136] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.1.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 60755 invoked from network); 3 Jan 2017 23:29:34 -0000 Received: from mail.kernel.org (HELO mail.kernel.org) (198.145.29.136) by server-6.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 3 Jan 2017 23:29:34 -0000 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 34F8A20379; Tue, 3 Jan 2017 23:29:31 +0000 (UTC) Received: from sstabellini-ThinkPad-X260.hsd1.ca.comcast.net (96-82-76-110-static.hfc.comcastbusiness.net [96.82.76.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E5B2A200C6; Tue, 3 Jan 2017 23:29:28 +0000 (UTC) From: Stefano Stabellini To: julien.grall@arm.com Date: Tue, 3 Jan 2017 15:29:27 -0800 Message-Id: <1483486167-24607-1-git-send-email-sstabellini@kernel.org> X-Mailer: git-send-email 1.9.1 X-Virus-Scanned: ClamAV using ClamSMTP Cc: xen-devel@lists.xenproject.org, sstabellini@kernel.org Subject: [Xen-devel] [PATCH v3] xen/arm: fix rank/vgic lock inversion bug 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 Always set the new physical irq affinity at the beginning of vgic_migrate_irq, in all cases. No need to set physical irq affinity in gic_update_one_lr anymore, solving the lock inversion problem. After migrating an interrupt from vcpu/pcpu 0 to vcpu/pcpu 1, it is possible to receive a physical interrupt on pcpu 1, which Xen is supposed to inject into vcpu 1, before the LR on pcpu 0 has been cleared. In this case the irq is still marked as GIC_IRQ_GUEST_MIGRATING, and struct pending_irq is still "inflight" on vcpu 0. As the irq cannot be "inflight" on vcpu 0 and vcpu 1 simultaneously, drop the interrupt. Coverity-ID: 1381855 Coverity-ID: 1381853 Signed-off-by: Stefano Stabellini --- xen/arch/arm/gic.c | 6 +----- xen/arch/arm/vgic.c | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/xen/arch/arm/gic.c b/xen/arch/arm/gic.c index a5348f2..767fc9e 100644 --- a/xen/arch/arm/gic.c +++ b/xen/arch/arm/gic.c @@ -504,11 +504,7 @@ static void gic_update_one_lr(struct vcpu *v, int i) gic_raise_guest_irq(v, irq, p->priority); else { list_del_init(&p->inflight); - if ( test_and_clear_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) - { - struct vcpu *v_target = vgic_get_target_vcpu(v, irq); - irq_set_affinity(p->desc, cpumask_of(v_target->processor)); - } + clear_bit(GIC_IRQ_GUEST_MIGRATING, &p->status); } } } diff --git a/xen/arch/arm/vgic.c b/xen/arch/arm/vgic.c index 364d5f0..11ffb9b 100644 --- a/xen/arch/arm/vgic.c +++ b/xen/arch/arm/vgic.c @@ -264,20 +264,17 @@ void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq) if ( p->desc == NULL ) return; + irq_set_affinity(p->desc, cpumask_of(new->processor)); + /* migration already in progress, no need to do anything */ if ( test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) return; + if ( list_empty(&p->inflight) ) + return; perfc_incr(vgic_irq_migrates); spin_lock_irqsave(&old->arch.vgic.lock, flags); - - if ( list_empty(&p->inflight) ) - { - irq_set_affinity(p->desc, cpumask_of(new->processor)); - spin_unlock_irqrestore(&old->arch.vgic.lock, flags); - return; - } /* If the IRQ is still lr_pending, re-inject it to the new vcpu */ if ( !list_empty(&p->lr_queue) ) { @@ -286,7 +283,6 @@ void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq) list_del_init(&p->inflight); irq_set_affinity(p->desc, cpumask_of(new->processor)); spin_unlock_irqrestore(&old->arch.vgic.lock, flags); - vgic_vcpu_inject_irq(new, irq); return; } /* if the IRQ is in a GICH_LR register, set GIC_IRQ_GUEST_MIGRATING @@ -495,6 +491,13 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq) return; } + if ( test_bit(GIC_IRQ_GUEST_MIGRATING, &n->status) ) + { + /* Drop the interrupt, because it is still inflight on another vcpu */ + spin_unlock_irqrestore(&v->arch.vgic.lock, flags); + return; + } + set_bit(GIC_IRQ_GUEST_QUEUED, &n->status); if ( !list_empty(&n->inflight) )