Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1832608imm; Mon, 3 Sep 2018 10:35:17 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYM0QRulBatNmri5kYIKpkFOHz3riqt5JG7UDksgfJZG627y3LSWTXDuqarDi/vIqlckHtQ X-Received: by 2002:a63:4b47:: with SMTP id k7-v6mr17174580pgl.351.1535996117668; Mon, 03 Sep 2018 10:35:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535996117; cv=none; d=google.com; s=arc-20160816; b=gsq0pfASmlt+6prucYUIN0DNWABUhLzegGCO7zjSR73fA2NLz3kM3V75mytJdj2ebV KYNfeg3G0cm0zpCPdot1Br0Cuc47ToKMqFr+cBpOuLaZC7J4rX7NtWmGKURD0tYK9ukX 0l90xNnWMUdxgzXCB4D0cFQMBBVnLDPA1IM9iXM3/ghn8Pcbrq1RZAurrqEqasMl7Qc0 mtX+9HIJhaCOkwohLK/7wOUBLsQonTp0D0aasaLRsTme8cDiz27Z7aDLcXmb3SZ7V1qv CvZJLh2FE7N2RoGzPLzx37WPRYifuLw9MW2JILe+coEkAw4etgE0Yvc9TSZr5HT6S0MJ 09/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=yShDZSH77AkE7MKbjrO9ZZYneEsP9zHATg5xq8UqOKg=; b=yoHM10HAaoENZ8whLEJFnBcs3+7pwuXlHTi1p//Fjn1kFyaBlB0tqNC48gHZei1Yt1 UzknHSlCfz3esyDKBpmD3qwEAnY2bp4iH8OnaWyy8O6O2VFQVSi6WadVwMThuaIS3P+f tJEj1hFHP7EHKh8R0QyawXb/RJHrj2UBttbIrkc2EPnt61Lc/AowXAt4NBLdZcS+0tnZ 46R1wt10a/KQadv4M3RCUCkXk4EA8ySneiPX9tQLBwcF4R/PZZSNd2+Er2W0htfjCTPM 2+gWfIghAm3J7eOsH6zqFyH5zx3JHaBY1cR/vIb7bSkZyu8o/MJHKiRqs3kcbqbPPCdZ V0NA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e5-v6si18306894pfg.258.2018.09.03.10.35.02; Mon, 03 Sep 2018 10:35:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731368AbeICVzF (ORCPT + 99 others); Mon, 3 Sep 2018 17:55:05 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:47718 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728601AbeICVzE (ORCPT ); Mon, 3 Sep 2018 17:55:04 -0400 Received: from localhost (ip-213-127-74-90.ip.prioritytelecom.net [213.127.74.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id A5CADD18; Mon, 3 Sep 2018 17:33:54 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christoffer Dall , Marc Zyngier Subject: [PATCH 4.18 033/123] KVM: arm/arm64: Fix potential loss of ptimer interrupts Date: Mon, 3 Sep 2018 18:56:17 +0200 Message-Id: <20180903165720.883684870@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180903165719.499675257@linuxfoundation.org> References: <20180903165719.499675257@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Christoffer Dall commit 7afc4ddbf299a13aaf28406783d141a34c6b4f5a upstream. kvm_timer_update_state() is called when changing the phys timer configuration registers, either via vcpu reset, as a result of a trap from the guest, or when userspace programs the registers. phys_timer_emulate() is in turn called by kvm_timer_update_state() to either cancel an existing software timer, or program a new software timer, to emulate the behavior of a real phys timer, based on the change in configuration registers. Unfortunately, the interaction between these two functions left a small race; if the conceptual emulated phys timer should actually fire, but the soft timer hasn't executed its callback yet, we cancel the timer in phys_timer_emulate without injecting an irq. This only happens if the check in kvm_timer_update_state is called before the timer should fire, which is relatively unlikely, but possible. The solution is to update the state of the phys timer after calling phys_timer_emulate, which will pick up the pending timer state and update the interrupt value. Note that this leaves the opportunity of raising the interrupt twice, once in the just-programmed soft timer, and once in kvm_timer_update_state. Since this always happens synchronously with the VCPU execution, there is no harm in this, and the guest ever only sees a single timer interrupt. Cc: Stable # 4.15+ Signed-off-by: Christoffer Dall Signed-off-by: Marc Zyngier Signed-off-by: Greg Kroah-Hartman --- virt/kvm/arm/arch_timer.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) --- a/virt/kvm/arm/arch_timer.c +++ b/virt/kvm/arm/arch_timer.c @@ -295,9 +295,9 @@ static void phys_timer_emulate(struct kv struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); /* - * If the timer can fire now we have just raised the IRQ line and we - * don't need to have a soft timer scheduled for the future. If the - * timer cannot fire at all, then we also don't need a soft timer. + * If the timer can fire now, we don't need to have a soft timer + * scheduled for the future. If the timer cannot fire at all, + * then we also don't need a soft timer. */ if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) { soft_timer_cancel(&timer->phys_timer, NULL); @@ -332,10 +332,10 @@ static void kvm_timer_update_state(struc level = kvm_timer_should_fire(vtimer); kvm_timer_update_irq(vcpu, level, vtimer); + phys_timer_emulate(vcpu); + if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); - - phys_timer_emulate(vcpu); } static void vtimer_save_state(struct kvm_vcpu *vcpu)