From patchwork Mon Dec 18 10:00:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10118681 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 687496019C for ; Mon, 18 Dec 2017 10:01:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57AFE28DB1 for ; Mon, 18 Dec 2017 10:01:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4CA1D28E35; Mon, 18 Dec 2017 10:01:33 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D6DC928DB1 for ; Mon, 18 Dec 2017 10:01:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933584AbdLRKBb (ORCPT ); Mon, 18 Dec 2017 05:01:31 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:38827 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758216AbdLRKBX (ORCPT ); Mon, 18 Dec 2017 05:01:23 -0500 Received: by mail-wm0-f66.google.com with SMTP id 64so28144653wme.3 for ; Mon, 18 Dec 2017 02:01:22 -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=UQbtw5WHqroza2YS6EdDYQsy7bcOCbBJJ9yD/YJXHIY=; b=kAuYImsKn/PsO1kAFgXfBiEF1FeXLuTLr7tmhdVdr4061V53C+k2vnk/Jg6DiIw1ZE yuDdpolquanlM/B1L3Bw92mTjdNgJ910+vEI7pjLgZ9eHde3EmCDXqXm93RE5jdDC3fs Y4FXMvry5uRhECTZzS6Wc/X/cZ8/NoIjH9pcY= 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=UQbtw5WHqroza2YS6EdDYQsy7bcOCbBJJ9yD/YJXHIY=; b=V3fMJFV5P8dILkDmlR5eOv6fq9V7XMZ/O8Su/yVU2W2/7L8Iisr1eW9cfcsHYQrves xa20hElvnCf2eq1XGdrxAf1szzLgESWyeLqIn2u2PasdN1JqC6GweynyyWgYZ1nofujN 6MWK+NL8Gfe1qfY5aXJTsfmWeRSrrDR9kFgcKOCCZZj53oMoxgWDNPPkKwIlPXrqqzuZ idB0Zd72QKuDj8g5lFti8Dzk1SGciqZIKqCascO38F0QcMoK+mt3/Nbb/Q5fO0ujA8Ad p+CaPxJtrOuINQALyWNWN+GCBW84BmuAvM53WhKuRsVbJAsJoF7A2ifg5/AfxYPTLsF8 a/ow== X-Gm-Message-State: AKGB3mK/VEZzVYV7o2mEZc910itgq8rw6UfgB9VjrpT0Lkwk1FtyY/wO WCZvtKixljQAcCacHvBtWbtXUqdQBsc= X-Google-Smtp-Source: ACJfBotppmkgpu/N3Simk3QJX/iUq4fnkYQuej8ifdJKQ7684mIiXkgbB3N+OWPUvskJ9UUnS/9BRQ== X-Received: by 10.80.149.21 with SMTP id u21mr29130851eda.137.1513591282053; Mon, 18 Dec 2017 02:01:22 -0800 (PST) Received: from localhost.localdomain (x50d2404e.cust.hiper.dk. [80.210.64.78]) by smtp.gmail.com with ESMTPSA id h16sm10403130edj.34.2017.12.18.02.01.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 18 Dec 2017 02:01:21 -0800 (PST) From: Christoffer Dall To: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Cc: Marc Zyngier , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, Christoffer Dall Subject: [PULL 4/5] KVM: arm/arm64: Properly handle arch-timer IRQs after vtimer_save_state Date: Mon, 18 Dec 2017 11:00:56 +0100 Message-Id: <20171218100057.7839-5-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171218100057.7839-1-christoffer.dall@linaro.org> References: <20171218100057.7839-1-christoffer.dall@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.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 Reviewed-by: Marc Zyngier Tested-by: Marc Zyngier 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: