From patchwork Fri Sep 15 22:19:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 9953885 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 4CCC4601C0 for ; Fri, 15 Sep 2017 22:23:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36C882957A for ; Fri, 15 Sep 2017 22:23:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B0F92957D; Fri, 15 Sep 2017 22:23:11 +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,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED 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 C6B8429581 for ; Fri, 15 Sep 2017 22:23:10 +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:References: In-Reply-To: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:List-Owner; bh=NhZNGTh5rp3vMJD8yWvgeIb6SQ5PaQzCYZRRMFJHLJ0=; b=GFFo+jv7j5PLTwlzpCmJ2eHc0E wVuUbuSOKNGcmhgXCUJj9Z6U41f9auXeECfO/Q9+0lmRawnS21VrcXF2CwqLtxA5OT56MviSNxJZD 1PI0LeQFoPfHuVc78Ld+WeulaVgc9b4D8tZO5i8+jGhd6JykUSM37lPVkbPAKOGwF7/2/8+yeBXHU gNuDbjkVUZ+FPj2WXibblBN7CdWY2gmNI4LIUmzz4u+KYI3Z6PENnEfdR95Zf9H1nR43+VMeu2+s4 LeNGZSib9aogoD1JosCOMhsVw2VF8wc4X33UBdCRvHei5Rxg+sexgiO6Mq6YknleJpswfHRXXQI0U Jc/FX4aA==; 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 1dsz0D-0004aj-C7; Fri, 15 Sep 2017 22:22:25 +0000 Received: from mail-ua0-x22d.google.com ([2607:f8b0:400c:c08::22d]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dsyyP-0002Ce-GZ for linux-arm-kernel@lists.infradead.org; Fri, 15 Sep 2017 22:20:43 +0000 Received: by mail-ua0-x22d.google.com with SMTP id 97so1917470uai.6 for ; Fri, 15 Sep 2017 15:20:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3p8ZfPAMC2EvPW3mrTlTb0DQmcguLsSmwdVm2pGiVE0=; b=QVnaHnwVD0sa890zy6+wP/3dZ52fp+YZPAltrht40UfzzDKtkT58iDGOLPIThWUoQY dSwHQZeXcgq8n9tXEESDoCmlXQuWVFCfFsk/T1AAQmlIsP7RC+DYdrS3qAo9YFjPFr4A 6YepYhVl+iX7mfdbyPLxCKPyqoNFNLV3hi6EQ= 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:in-reply-to :references; bh=3p8ZfPAMC2EvPW3mrTlTb0DQmcguLsSmwdVm2pGiVE0=; b=NpFsT3ymaO4y8AtVEDaytW1l7xdDVHydZaG5PPf2MFHVcX1BlPRn3dYVaTyoz0FlmT E3BCO6U1B+qaPxxQPLetoonZccJAfvXFt1qOnxcDI5/n69zwWsUPf9QRyLDKUyj69lWN x17DpkLvL3myRkJeAPkBxeWCYqcHIE2zaU/4rhL2VyGowoiUTXkN+f9JhZiw+h2LGyyZ wp30Z5YcnDVvWs5BaR1NoIJ+WJNfyKRW9MzLYDFP52ss32cjKOpOCsn5sQhWs/xWGMC/ UmFsT9JjqkT0arl4djIR91d3t+cmR2WBGPwu6MSbcKyB7vfgMVO8NvDDeu+hauw35aJh IFzw== X-Gm-Message-State: AHPjjUhSryr4umxGce3M1U/rZf+/O/SGX1iYFSlzF3UEAc0BBY7owtMk fW/+kZ7qxoaVB5eNWdh09g== X-Google-Smtp-Source: ADKCNb6/M9txEaLQxwpYsx1nXyzoWtvPQTdXh9YH2o5tgPAdLQAZ1dsZQHtwjsCaNPqpZXekhfnEEw== X-Received: by 10.176.10.29 with SMTP id q29mr20717937uah.133.1505514005517; Fri, 15 Sep 2017 15:20:05 -0700 (PDT) Received: from localhost.localdomain ([12.145.98.253]) by smtp.gmail.com with ESMTPSA id q64sm399435uaq.13.2017.09.15.15.20.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Sep 2017 15:20:04 -0700 (PDT) From: Christoffer Dall To: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 2/7] KVM: arm/arm64: Factor out functionality to get vgic mmio requester_vcpu Date: Fri, 15 Sep 2017 15:19:49 -0700 Message-Id: <1505513994-77939-3-git-send-email-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1505513994-77939-1-git-send-email-christoffer.dall@linaro.org> References: <1505513994-77939-1-git-send-email-christoffer.dall@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170915_152034_175063_73FA98F4 X-CRM114-Status: GOOD ( 16.31 ) 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 , Eric Auger , kvm@vger.kernel.org, Christoffer Dall 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: Christoffer Dall We are about to distinguish between userspace accesses and mmio traps for a number of the mmio handlers. When the requester vcpu is NULL, it mens we are handling a userspace acccess. Factor out the functionality to get the request vcpu into its own function, mostly so we have a common place to document the semantics of the return value. Also take the chance to move the functionality outside of holding a spinlock and instead explicitly disable and enable preemption. This supports PREEMPT_RT kernels as well. Signed-off-by: Christoffer Dall Acked-by: Marc Zyngier --- virt/kvm/arm/vgic/vgic-mmio.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/virt/kvm/arm/vgic/vgic-mmio.c b/virt/kvm/arm/vgic/vgic-mmio.c index c1e4bdd..f3087f6 100644 --- a/virt/kvm/arm/vgic/vgic-mmio.c +++ b/virt/kvm/arm/vgic/vgic-mmio.c @@ -120,6 +120,26 @@ unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu, return value; } +/* + * This function will return the VCPU that performed the MMIO access and + * trapped from twithin the VM, and will return NULL if this is a userspace + * access. + * + * We can disable preemption locally around accessing the per-CPU variable + * because even if the current thread is migrated to another CPU, reading the + * per-CPU value later will give us the same value as we update the per-CPU + * variable in the preempt notifier handlers. + */ +static struct kvm_vcpu *vgic_get_mmio_requester_vcpu(void) +{ + struct kvm_vcpu *vcpu; + + preempt_disable(); + vcpu = kvm_arm_get_running_vcpu(); + preempt_enable(); + return vcpu; +} + void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val) @@ -180,23 +200,9 @@ 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); + struct kvm_vcpu *requester_vcpu = vgic_get_mmio_requester_vcpu(); - /* - * 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(); + spin_lock(&irq->irq_lock); /* * If this virtual IRQ was written into a list register, we @@ -208,6 +214,11 @@ static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq, * 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. + * + * When accessing VGIC state from user space, 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. */ while (irq->vcpu && /* IRQ may have state in an LR somewhere */ irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */