From patchwork Fri Dec 15 14:16:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10115257 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 4B63760231 for ; Fri, 15 Dec 2017 14:29:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4038E29F80 for ; Fri, 15 Dec 2017 14:29:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 34B5929F85; Fri, 15 Dec 2017 14:29:58 +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 BBCA929F80 for ; Fri, 15 Dec 2017 14:29:57 +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=RZGIEEqQOCqSlcA2fG5P45rz9gMdOfsN0ZEsF9UXjws=; b=rcEj8usKjpbBDzxOZ6Xu+D4C9v qyl1554+kuk66v/xc9OxfsWiEItdSnlh3CF9iwbc0h1GR5UVN2bzIOjNDn6ssI/jzui+qti7u0gxR sx/IxIL3CsVHdKF91y18YaUBPAPRseZ5ONQrRG4mhmc+Njh+0TurJtAzY7O5NTDnvydGX/tP7Nci/ yd6KyYMXKUnzUWikUo7DTMUJo2FTo60iVw22Dv0f/OsW9bu36rqfKLa1ePKHR6XVTvwg5xAaQ2ZDQ yO30CTr7ER6tqzNQDrncbcEdNdOcRqWxGWouuk6G53Lgg1c4vjK0B5g4yB0acY54FT8yGQDPPY4ML KCNWSxRg==; 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 1ePqzt-0004aA-0v; Fri, 15 Dec 2017 14:29:57 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ePqnq-0004vH-T6 for linux-arm-kernel@lists.infradead.org; Fri, 15 Dec 2017 14:17:33 +0000 Received: by mail-wm0-x244.google.com with SMTP id i11so17816106wmf.4 for ; Fri, 15 Dec 2017 06:17:11 -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=NbneT4BPv8pf/fOxUxvtc4efbO3iuiZzK/A1C8grBzk=; b=YZVUGw3kKvXGfIcA8ThSwolCmBJpP1ATWo3jsoeSnMnlHHWzbR/lXUpU3EcQ9wAJnG BZC+hcS6Is2qCVbt4ltmJGY7AaqyZ/2Uu4gFQ7/kTsPZhlygHUmYdV23lpn+yZCpqVad RTQcoVfDk+qstTci/9cQkQB9Qy8xTSx82gGF0= 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=NbneT4BPv8pf/fOxUxvtc4efbO3iuiZzK/A1C8grBzk=; b=IYoa+1n3uAq8xnJqeH5wyhTq7St0o2JYgR2jElf53Z9058pAdr+yeRx/+osnoLXOqA 0EHtO2erRu5x2BOeSpqf/ETvOv9O+ty9MVU8axm504Q19R0utUanDn09KytDnpRYejeq pmZD1VT+PQZDU8ZgQrzThyX8r2euYJnSGB3NQ2NNqetoWXoj8SuCDPwJ5wPar8rIxia0 WS420Bk6Ur2MXax8OkZ1LURWs7QJKAkM0V82WqQaJpxX8VuGOj+SJOdE559WJTzZWY3o d+ifuHLAC8PsqUJbWncWEIW5KVWAZHkUze9x/N5YShoGyNahfMuF78JN7n3aaQBLKQ5w CUTw== X-Gm-Message-State: AKGB3mK8bZ00zAuvh2/u3lpHZ2jEHpEcU/XrfAtz+t+WDynYY8Gb3Fpe kuS5SZrqS+6Yzj8lCKb+GfDMeQ== X-Google-Smtp-Source: ACJfBouhVfUJPeUVQ7U8IpUBb3u0bGyj0ElKzTvQkkUV3ljGFGO/EjIsKFUS9wByCg0tplulciA3LQ== X-Received: by 10.80.163.219 with SMTP id t27mr18109309edb.248.1513347430298; Fri, 15 Dec 2017 06:17:10 -0800 (PST) Received: from localhost.localdomain (x50d2404e.cust.hiper.dk. [80.210.64.78]) by smtp.gmail.com with ESMTPSA id 4sm5293320edf.81.2017.12.15.06.17.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Dec 2017 06:17:09 -0800 (PST) From: Christoffer Dall To: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Subject: [PATCH 1/2] KVM: arm/arm64: Properly handle arch-timer IRQs after vtimer_save_state Date: Fri, 15 Dec 2017 15:16:55 +0100 Message-Id: <20171215141656.25815-2-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171215141656.25815-1-christoffer.dall@linaro.org> References: <20171215141656.25815-1-christoffer.dall@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171215_061731_149741_C0FCA181 X-CRM114-Status: GOOD ( 16.49 ) 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 , Jia He , Christoffer Dall , kvm@vger.kernel.org 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 The recent timer rework was assuming that once the timer was disabled, we should no longer see any interrupts from the timer. This assumption turns out to not be true, and instead we have to handle the case when the timer ISR runs even after the timer has been disabled. This requires a couple of changes: First, we should never overwrite the cached guest state of the timer control register when the ISR runs, because KVM may have disabled its timers when doing vcpu_put(), even though the guest still had the timer enabled. Second, we shouldn't assume that the timer is actually firing just because we see an interrupt, but we should check the actual state of the timer in the timer control register to understand if the hardware timer is really firing or not. We also add an ISB to vtimer_save_state() to ensure the timer is actually disabled once we enable interrupts, which should clarify the intention of the implementation, and reduce the risk of unwanted interrupts. Fixes: b103cc3f10c0 ("KVM: arm/arm64: Avoid timer save/restore in vcpu entry/exit") Reported-by: Marc Zyngier Reported-by: Jia He Signed-off-by: Christoffer Dall --- virt/kvm/arm/arch_timer.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c index aa9adfafe12b..14c018f990a7 100644 --- a/virt/kvm/arm/arch_timer.c +++ b/virt/kvm/arm/arch_timer.c @@ -92,16 +92,23 @@ static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id) { struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id; struct arch_timer_context *vtimer; + u32 cnt_ctl; - if (!vcpu) { - pr_warn_once("Spurious arch timer IRQ on non-VCPU thread\n"); - return IRQ_NONE; - } - vtimer = vcpu_vtimer(vcpu); + /* + * We may see a timer interrupt after vcpu_put() has been called which + * sets the CPU's vcpu pointer to NULL, because even though the timer + * has been disabled in vtimer_save_state(), the hardware interrupt + * signal may not have been retired from the interrupt controller yet. + */ + if (!vcpu) + return IRQ_HANDLED; + vtimer = vcpu_vtimer(vcpu); if (!vtimer->irq.level) { - vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl); - if (kvm_timer_irq_can_fire(vtimer)) + cnt_ctl = read_sysreg_el0(cntv_ctl); + cnt_ctl &= ARCH_TIMER_CTRL_ENABLE | ARCH_TIMER_CTRL_IT_STAT | + ARCH_TIMER_CTRL_IT_MASK; + if (cnt_ctl == (ARCH_TIMER_CTRL_ENABLE | ARCH_TIMER_CTRL_IT_STAT)) kvm_timer_update_irq(vcpu, true, vtimer); } @@ -355,6 +362,7 @@ static void vtimer_save_state(struct kvm_vcpu *vcpu) /* Disable the virtual timer */ write_sysreg_el0(0, cntv_ctl); + isb(); vtimer->loaded = false; out: