From patchwork Sat Nov 25 20:57:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10075179 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 6E12D60353 for ; Sat, 25 Nov 2017 21:16:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5E56628FEB for ; Sat, 25 Nov 2017 21:16:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 51E6228FED; Sat, 25 Nov 2017 21:16:17 +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 E6FCC28FEB for ; Sat, 25 Nov 2017 21:16:16 +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=wVDGQvbzn3OUO/O/Z36gdaw4PJU7pMKprJtbPZV3VFU=; b=MDx2LraJWvKEyXXsYzdRAmlLWJ 4m3j0fsyfiIl+T0OIp+QQV6GAU9RViIiwINLEmg5L3tkvuE/AbHG7Wmd0cHxZZrS5BWlHp46l/sTG RF7H4e5d4IAtPNeEQUtvCRMPI5rt/Bn7TEo6CmUNWXO+Bw/23GxcCDYpAbxd+Bfs2jRW2HLrkDodC WY5GGyoPW93/hYpFxTiHqZ15d7AkMAzXDP3ZEFCEXYIfygVpBeidK0n9xaNVwAnlygLWrhwK/+Elh NVoWEYnK0JK/+dWmOGudCEhqLh1RpeF/xUs9g9U4Nz/9rT6RLpZ8kG8vsQLYgLPNptSRJ316SRhfW tDiSpOaw==; 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 1eIho7-00081I-I8; Sat, 25 Nov 2017 21:16:15 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eIhWQ-0005CJ-L4 for linux-arm-kernel@lists.infradead.org; Sat, 25 Nov 2017 20:58:05 +0000 Received: by mail-wm0-x243.google.com with SMTP id x63so27595432wmf.2 for ; Sat, 25 Nov 2017 12:57:39 -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:in-reply-to:references; bh=dmh69YdXQ9v239cQEb7aUvanC5tbQkPr6uCJ6FFCp20=; b=NYF/C4lDPkh150iGl2i1WfVw0h82y3DAl4zpI1/bQQKhzlvn9jKPMTl3HEQzHigl+5 f8OgN5RJTJRL/qGwHOJRlMSxLd57GLkjZJ2Nn04F6h0nSIe6kXoWHn8KjstDp7E8JHZF LaEiTR3P0W+zxPt9U2KMam6PjX9XTpxNH7bnM= 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=dmh69YdXQ9v239cQEb7aUvanC5tbQkPr6uCJ6FFCp20=; b=N1uDdOYPh/G02S74zICCnQDZCzTWQNJGormSo7eGEv/N2bt9iZ0OSP+89md7UP+bJM 9Ggnb3LVtIVES0Ywe418J2ozUK5VT/hXqNn4fBlUiKxHQhPdzI7OyPzm4u5YQOKH00rq zTNGMFvbnYnTeC4VYbL5tDfUneLp0DWR7wfbsPa2I/2tHKd7wSMP7e6bV/N6VWfbpcbK 7P8vMGnJ0OChfRt/Ldj90YHYpaM0DcdPvnOwPaLYXCxN7tWHBgT+rjI/zVpY51nOQfkb E5m6hkE+b8oY+CqCXrzwr+8FZzcx+wrzzDYWHg0zfWoLkUbVe4aKwIVUH2GGZ9dz5eNV CZiQ== X-Gm-Message-State: AJaThX4nW034Eja/WRGFPDVspAiPh4VJV7HLxkmpLB97lstLW4xTrAyx TZENBkO4a3UL/dvfI6sKXkqcOA== X-Google-Smtp-Source: AGs4zMZPZzh+/lkBGu/qYcT6IdSyKm4cwvAREsGOp3hedZf3kY7NWWinMXxyY9t4/BzoPGTTEB+kmw== X-Received: by 10.28.132.213 with SMTP id g204mr4653790wmd.90.1511643458400; Sat, 25 Nov 2017 12:57:38 -0800 (PST) Received: from localhost.localdomain (x50d2404e.cust.hiper.dk. [80.210.64.78]) by smtp.gmail.com with ESMTPSA id z37sm15157577wrc.31.2017.11.25.12.57.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 25 Nov 2017 12:57:37 -0800 (PST) From: Christoffer Dall To: kvm@vger.kernel.org Subject: [PATCH 15/15] KVM: arm/arm64: Avoid vcpu_load for other vcpu ioctls than KVM_RUN Date: Sat, 25 Nov 2017 21:57:18 +0100 Message-Id: <20171125205718.7731-16-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171125205718.7731-1-christoffer.dall@linaro.org> References: <20171125205718.7731-1-christoffer.dall@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171125_125759_301298_67DE2BCF X-CRM114-Status: GOOD ( 14.85 ) 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: linux-mips@linux-mips.org, Andrew Jones , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Marc Zyngier , James Hogan , Cornelia Huck , Christian Borntraeger , kvm-ppc@vger.kernel.org, Alexander Graf , linux-arm-kernel@lists.infradead.org, Paolo Bonzini , linux-s390@vger.kernel.org, kvmarm@lists.cs.columbia.edu, 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 Calling vcpu_load() takes the vcpu->mutex, registers preempt notifiers for this vcpu, and calls kvm_arch_vcpu_load(). The latter will soon be doing a lot of heavy lifting on arm/arm64 and will try to do things such as enabling the virtual timer and setting us up to handle interrupts from the timer hardware. Loading state onto hardware registers and enabling hardware to signal interrupts can be problematic when we're not actually about to run the VCPU, because it makes it difficult to establish the right context when handling interrupts from the timer, and it makes the register access code difficult to reason about. Luckily, now when we call vcpu_load in each ioctl implementation, we can simply change the non-KVM_RUN vcpu ioctls to only take the vcpu->mutex instead of calling vcpu_load(), and our kvm_arch_vcpu_load() is only used for loading vcpu content to the physical CPU when we're actually going to run the vcpu. Signed-off-by: Christoffer Dall --- arch/arm64/kvm/guest.c | 8 ++++---- virt/kvm/arm/arm.c | 25 ++++++++++--------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 0375d1f977c8..891d5c5e6e4f 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -363,9 +363,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, { int ret; - ret = vcpu_load(vcpu); - if (ret) - return ret; + if (mutex_lock_killable(&vcpu->mutex)) + return -EINTR; trace_kvm_set_guest_debug(vcpu, dbg->control); @@ -387,8 +386,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, vcpu->guest_debug = 0; } + ret = 0; out: - vcpu_put(vcpu); + mutex_unlock(&vcpu->mutex); return ret; } diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index a1b486a71e85..5095366d3b21 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -381,29 +381,25 @@ static void vcpu_power_off(struct kvm_vcpu *vcpu) int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - int ret; - - ret = vcpu_load(vcpu); - if (ret) - return ret; + if (mutex_lock_killable(&vcpu->mutex)) + return -EINTR; if (vcpu->arch.power_off) mp_state->mp_state = KVM_MP_STATE_STOPPED; else mp_state->mp_state = KVM_MP_STATE_RUNNABLE; - vcpu_put(vcpu); + mutex_unlock(&vcpu->mutex); return 0; } int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - int ret; + int ret = 0; - ret = vcpu_load(vcpu); - if (ret) - return ret; + if (mutex_lock_killable(&vcpu->mutex)) + return -EINTR; switch (mp_state->mp_state) { case KVM_MP_STATE_RUNNABLE: @@ -416,7 +412,7 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, ret = -EINVAL; } - vcpu_put(vcpu); + mutex_unlock(&vcpu->mutex); return ret; } @@ -1009,9 +1005,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp, struct kvm_device_attr attr; long r; - r = vcpu_load(vcpu); - if (r) - return r; + if (mutex_lock_killable(&vcpu->mutex)) + return -EINTR; switch (ioctl) { case KVM_ARM_VCPU_INIT: { @@ -1089,7 +1084,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r = -EINVAL; } - vcpu_put(vcpu); + mutex_unlock(&vcpu->mutex); return r; }