From patchwork Fri Oct 27 08:34:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10029497 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 C51006034B for ; Fri, 27 Oct 2017 09:50:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B9BEF28F82 for ; Fri, 27 Oct 2017 09:50:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ADE9828F84; Fri, 27 Oct 2017 09:50:27 +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 27CEA28F82 for ; Fri, 27 Oct 2017 09:50:27 +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=Gvr973TqjCmH7sPBXpWYdbl0TWixZT2ZaFK8ZqY/0bE=; b=SuJHoplAHMesgIHv+x3161lQrY tHoKNe/CZUHOEE+l+fVFPhkyjqNauAGmI56gDpg+9FtaFJjmzU9APDQl0k8nvDyezIiwpdhB2YQ3f L5JsLMOTkgdPMFAQjTeFoPem+EF0l5mhf/BLL/FhxAoUyJEbfD8ZF7XEmWCT2iorRjfK6OPYsx7dN GLhecxwu5BKgypTp3eXPyCnEv6jM3PyO27irAzjiBd4M7tMGQcdPzRgXV/TS5KczefXhd0SdnXe4x bHxvfRtABs/efN7ugXVwqRPmHWgp+fj+PIKtc88yfQCPUwpeuBe5Hc2fysDk+yu/nsWUqNujYdoVf jT1j3k+g==; 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 1e81HW-00077n-F6; Fri, 27 Oct 2017 09:50:26 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e81GM-0004zZ-Hp for linux-arm-kernel@bombadil.infradead.org; Fri, 27 Oct 2017 09:49:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Ek1iIZvm2dG/MaPSRXHYDpiOy97Cfdh+y8JIFYty1h0=; b=yRRnriO77jkPQwL4kopoEwNvU ILH5pzLVUdz95AyutDRSdNoA5x728isID0ozhMd6rsFS0vnFD9w+ZQPOeLWSNPuwxdpQmxk+E5pof cmwqd/5j04zqgrE8IEGBfp7e5VKjJKEK6PtvCLAj4/3UE4tp1z1zKTq2G37jGM1KFBfnM5cXQs1lJ /T4Zga2EOzt0P2a1Q6S4t/+YwMyAzTFO1BzhKLSbHECDbMwp3BVFRNxFLDQcNynUo6pFv8TLRU1x/ HD8zKGGgjcWlons9EnJbiwEXbhS0j3VMYNDN1I7Li9Yh8xORfc/csHnenSpmk87i2If/Z+iILZNqN 9/qJStM4g==; Received: from mail-lf0-x241.google.com ([2a00:1450:4010:c07::241]) by merlin.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e808P-0003mY-Ey for linux-arm-kernel@lists.infradead.org; Fri, 27 Oct 2017 08:36:58 +0000 Received: by mail-lf0-x241.google.com with SMTP id a132so6540152lfa.7 for ; Fri, 27 Oct 2017 01:36:37 -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=Ek1iIZvm2dG/MaPSRXHYDpiOy97Cfdh+y8JIFYty1h0=; b=J2t0klyl13+luhbcpy1ZLCiL1XKPHWI0eCWaZzoPpLxI8RAoOI+wPFlClYyqn9MyYP B5OCjZuS+xjcRbSeiN4SWtX9fq3eC5sk7Ojn3EEEkPX+7kIm6ObmPj27KeZR+gfTb9IX ptRqNtSEPQZpHTXJAd98E5esUSRipWKgWWs8k= 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=Ek1iIZvm2dG/MaPSRXHYDpiOy97Cfdh+y8JIFYty1h0=; b=coSK7aJA0J6q/atcQbDgVxAyWwA+sTVdJfOhwWw6wHDl8wGfz+gS+PpiolD0xImRFq 5eUQ+VeoZfxegSEisRJhug6XmXZbj0vLXq7PtfJJAD7esdmG4wOiu/wZQPhNBthHd0IQ 5+R1EEBLd7zlFeamDk/ArrYXUsEFcaqfDINbp43NyuPnuw2r5GqHAlcXI/ssOMt+M/cV ABiCp6aZ9uL7gRxagAbBvupxXoMvtS7ItazEZLFlOjS5I25PZ4ulYnV5JUdVjrpZght9 PeDTOTz3cDSgQuixXf4Jsfkj7HJwQpWfBnk9jvY8awqY5isycg18ozJ3zqROaVBM13Pl HVRw== X-Gm-Message-State: AMCzsaWBnFoMO3KF1AylrJ46Kyiuo4Ovnf7NiC0XoxNcYP7TmUpqrglG +HQU9d1PNFCXxEfL1AaBc0s/xg== X-Google-Smtp-Source: ABhQp+RsoQ0YkkhBND6r71LWGhRWgUKPTCGCnWaDeW3RS/RyuSxDapXuXdgUFl2K53naOMnlJsi2YQ== X-Received: by 10.25.26.143 with SMTP id a137mr847311lfa.105.1509093395072; Fri, 27 Oct 2017 01:36:35 -0700 (PDT) Received: from localhost.localdomain (212.27.17.163.bredband.3.dk. [212.27.17.163]) by smtp.gmail.com with ESMTPSA id d204sm1471957lfe.73.2017.10.27.01.36.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 27 Oct 2017 01:36:34 -0700 (PDT) From: Christoffer Dall To: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Subject: [PATCH v5 18/20] KVM: arm/arm64: Avoid phys timer emulation in vcpu entry/exit Date: Fri, 27 Oct 2017 10:34:39 +0200 Message-Id: <1509093281-15225-19-git-send-email-cdall@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1509093281-15225-1-git-send-email-cdall@linaro.org> References: <1509093281-15225-1-git-send-email-cdall@linaro.org> 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: Christoffer Dall , kvm@vger.kernel.org, Marc Zyngier , Catalin Marinas , Will Deacon , Eric Auger 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 There is no need to schedule and cancel a hrtimer when entering and exiting the guest, because we know when the physical timer is going to fire when the guest programs it, and we can simply program the hrtimer at that point. Now when the register modifications from the guest go through the kvm_arm_timer_set/get_reg functions, which always call kvm_timer_update_state(), we can simply consider the timer state in this function and schedule and cancel the timers as needed. This avoids looking at the physical timer emulation state when entering and exiting the VCPU, allowing for faster servicing of the VM when needed. Signed-off-by: Christoffer Dall Reviewed-by: Marc Zyngier --- virt/kvm/arm/arch_timer.c | 75 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c index 7f4f12d..20e56c4 100644 --- a/virt/kvm/arm/arch_timer.c +++ b/virt/kvm/arm/arch_timer.c @@ -202,7 +202,27 @@ static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt) static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt) { - WARN(1, "Timer only used to ensure guest exit - unexpected event."); + struct arch_timer_context *ptimer; + struct arch_timer_cpu *timer; + struct kvm_vcpu *vcpu; + u64 ns; + + timer = container_of(hrt, struct arch_timer_cpu, phys_timer); + vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); + ptimer = vcpu_ptimer(vcpu); + + /* + * Check that the timer has really expired from the guest's + * PoV (NTP on the host may have forced it to expire + * early). If not ready, schedule for a later time. + */ + ns = kvm_timer_compute_delta(ptimer); + if (unlikely(ns)) { + hrtimer_forward_now(hrt, ns_to_ktime(ns)); + return HRTIMER_RESTART; + } + + kvm_timer_update_irq(vcpu, true, ptimer); return HRTIMER_NORESTART; } @@ -256,24 +276,28 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, } /* Schedule the background timer for the emulated timer. */ -static void phys_timer_emulate(struct kvm_vcpu *vcpu, - struct arch_timer_context *timer_ctx) +static void phys_timer_emulate(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; + struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); - if (kvm_timer_should_fire(timer_ctx)) - return; - - if (!kvm_timer_irq_can_fire(timer_ctx)) + /* + * 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 (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) { + soft_timer_cancel(&timer->phys_timer, NULL); return; + } - /* The timer has not yet expired, schedule a background timer */ - soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(timer_ctx)); + soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer)); } /* - * Check if there was a change in the timer state (should we raise or lower - * the line level to the GIC). + * Check if there was a change in the timer state, so that we should either + * raise or lower the line level to the GIC or schedule a background timer to + * emulate the physical timer. */ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) { @@ -295,6 +319,8 @@ static void kvm_timer_update_state(struct kvm_vcpu *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) @@ -441,6 +467,9 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) if (has_vhe()) disable_el1_phys_timer_access(); + + /* Set the background timer for the physical timer emulation. */ + phys_timer_emulate(vcpu); } bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu) @@ -476,12 +505,6 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu) if (unlikely(!timer->enabled)) return; - - if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) - kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); - - /* Set the background timer for the physical timer emulation. */ - phys_timer_emulate(vcpu, vcpu_ptimer(vcpu)); } void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) @@ -497,6 +520,17 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) vtimer_save_state(vcpu); /* + * Cancel the physical timer emulation, because the only case where we + * need it after a vcpu_put is in the context of a sleeping VCPU, and + * in that case we already factor in the deadline for the physical + * timer when scheduling the bg_timer. + * + * In any case, we re-schedule the hrtimer for the physical timer when + * coming back to the VCPU thread in kvm_timer_vcpu_load(). + */ + soft_timer_cancel(&timer->phys_timer, NULL); + + /* * The kernel may decide to run userspace after calling vcpu_put, so * we reset cntvoff to 0 to ensure a consistent read between user * accesses to the virtual counter and kernel access to the physical @@ -538,16 +572,9 @@ static void unmask_vtimer_irq(struct kvm_vcpu *vcpu) */ void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) { - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); /* - * This is to cancel the background timer for the physical timer - * emulation if it is set. - */ - soft_timer_cancel(&timer->phys_timer, NULL); - - /* * If we entered the guest with the vtimer output asserted we have to * check if the guest has modified the timer so that we should lower * the line at this point.