From patchwork Mon Mar 6 13:42:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 9606059 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 E3E1E6046A for ; Mon, 6 Mar 2017 13:43:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D22192723E for ; Mon, 6 Mar 2017 13:43:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C71EA28418; Mon, 6 Mar 2017 13:43:09 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 581742723E for ; Mon, 6 Mar 2017 13:43:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=QN5nZrsyqYpswvZVB9RFnUrDc/Pv6+ZucxCs6QbkOfU=; b=B36 p00xdWfjnzde9gCk8WYeHrOiGMzivB5ftiMHqyxd1T/bJS9DQtio4G+F15/OryyZMUi09rbeCxvUn lmQ6zlvRPcLr+UCOnATDE67ZSkuK2mlnDBH4GLDMpGogiMhGW98jIE5q1LonleEobLEFE2Bij4efp 37dPyBpb3/e6Ogoz5nV6XOsLkklo/nGfRIGNE88qVPYpXV+PtyFY3wgOGicTks/3OudzEGhKT/huu nCJjIfLlO2z2ct3r3dNHryg7TgdOPAj3C0BQY12GojfEybXEbcEtmBMeyuN/2h6HeFC9M62CFLbxZ I6ay09CxENfFcCHDDeRuMFT4PYGaCaQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cksuq-0005bL-4g; Mon, 06 Mar 2017 13:43:08 +0000 Received: from mail-wr0-x22e.google.com ([2a00:1450:400c:c0c::22e]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cksum-0005XA-5p for linux-arm-kernel@lists.infradead.org; Mon, 06 Mar 2017 13:43:06 +0000 Received: by mail-wr0-x22e.google.com with SMTP id l37so116900911wrc.1 for ; Mon, 06 Mar 2017 05:42:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=K4FHei5NiBkDLJBcBo9H7EtOGh1z5gWT3bgFZJfruNc=; b=AkrkBIfRkvySlcQv14C1d6pEHs54QtNboyH9Qie/7ppQusjtBjXrZgjICJe279FXLV FpopIKndf4HFWRVX2sxhX9IvFFBF3ozWFGwKAAU8oJln+p0RcRvidA710SdZJwRyNmfu lnB7IKdLOYRzt+gecSn4AWkJ8jBFtU6iu89AM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=K4FHei5NiBkDLJBcBo9H7EtOGh1z5gWT3bgFZJfruNc=; b=Qj9AlDo6xOHVuiRBmpI0XrZkG2+QLJHuaYFXKEfCtnQEUL33xBJK5fUU1cr8c9Dp7Z 14fXkuw+OQ3iJqPQiqXzqTID1W7BTUcXtGgbf+1T0Y3aW7C8/rxZRhf0KQFbf53GO+/H +0Mg88l9BA+cy8Y8SJcP/a0L7Av8tGW0SNF7G6oCM6aVZpiMNLiAwBdvDRyUy1vJY86u 58vGL4+zWBPkLs/pa9jatgRdLNiUQjCyMO4TAjUU1oUQshrH/7ACEKElhFqffS6tAiGr +jroaZlX/McluB1DwIm/ePN/34oCmMdkTerc7w2u/oAV4KzjcT4aFCz3RFfRTtntYKqQ 5Wdg== X-Gm-Message-State: AMke39k+jHW/cyV2ee6/vhiAnltL9OLyMeZ63P1ps7x50taRzdJLO2AqRt8xMKFpso/YNd2O X-Received: by 10.223.167.66 with SMTP id e2mr14180327wrd.48.1488807761769; Mon, 06 Mar 2017 05:42:41 -0800 (PST) Received: from localhost.localdomain ([109.74.56.122]) by smtp.gmail.com with ESMTPSA id e72sm12568084wma.5.2017.03.06.05.42.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 06 Mar 2017 05:42:41 -0800 (PST) From: Christoffer Dall To: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Subject: [PATCH v2] KVM: arm/arm64: Let vcpu thread modify its own active state Date: Mon, 6 Mar 2017 05:42:37 -0800 Message-Id: <1488807757-86131-1-git-send-email-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.5.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170306_054304_518436_168D04D6 X-CRM114-Status: GOOD ( 16.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Marc Zyngier , Andre Przywara , Jintack Lim , kvm@vger.kernel.org, Eric Auger MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jintack Lim Currently, if a vcpu thread tries to change the active state of an interrupt which is already on the same vcpu's AP list, Since the VGIC mmio handler is called after a vcpu has already synced back the LR state to the struct vgic_irq, we can just let it proceed safely. Signed-off-by: Jintack Lim Reviewed-by: Marc Zyngier --- Changes since v1: - Reworked comment - Consider userspace accesses - Get the right requester VCPU for GICv3 private IRQ accesses - Tested using kvm-unit-tests and verified that it deadlocked without this patch and passed the test with this patch :) virt/kvm/arm/vgic/vgic-mmio.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/virt/kvm/arm/vgic/vgic-mmio.c b/virt/kvm/arm/vgic/vgic-mmio.c index 3654b4c..2a5db13 100644 --- a/virt/kvm/arm/vgic/vgic-mmio.c +++ b/virt/kvm/arm/vgic/vgic-mmio.c @@ -180,21 +180,37 @@ unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu, static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq, bool new_active_state) { + struct kvm_vcpu *requester_vcpu; spin_lock(&irq->irq_lock); + + /* + * The vcpu parameter here can mean multiple things depending on how + * this function is called; when handling a trap from the kernel it + * depends on the GIC version, and these functions are also called as + * part of save/restore from userspace. + * + * Therefore, we have to figure out the requester in a reliable way. + * + * When accessing VGIC state from user space, the requester_vcpu is + * NULL, which is fine, because we guarantee that no VCPUs are running + * when accessing VGIC state from user space so irq->vcpu->cpu is + * always -1. + */ + requester_vcpu = kvm_arm_get_running_vcpu(); + /* * If this virtual IRQ was written into a list register, we * have to make sure the CPU that runs the VCPU thread has - * synced back LR state to the struct vgic_irq. We can only - * know this for sure, when either this irq is not assigned to - * anyone's AP list anymore, or the VCPU thread is not - * running on any CPUs. + * synced back the LR state to the struct vgic_irq. * - * In the opposite case, we know the VCPU thread may be on its - * way back from the guest and still has to sync back this - * IRQ, so we release and re-acquire the spin_lock to let the - * other thread sync back the IRQ. + * As long as the conditions below are true, we know the VCPU thread + * may be on its way back from the guest (we kicked the VCPU thread in + * vgic_change_active_prepare) and still has to sync back this IRQ, + * so we release and re-acquire the spin_lock to let the other thread + * sync back the IRQ. */ while (irq->vcpu && /* IRQ may have state in an LR somewhere */ + irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */ irq->vcpu->cpu != -1) /* VCPU thread is running */ cond_resched_lock(&irq->irq_lock);