From patchwork Fri Jan 27 01:04:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9540495 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 C8E6460429 for ; Fri, 27 Jan 2017 01:08:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B4778281C3 for ; Fri, 27 Jan 2017 01:08:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A5E7828306; Fri, 27 Jan 2017 01:08:35 +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=-6.4 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 31383281C3 for ; Fri, 27 Jan 2017 01:08:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753694AbdA0BGt (ORCPT ); Thu, 26 Jan 2017 20:06:49 -0500 Received: from outprodmail01.cc.columbia.edu ([128.59.72.39]:57869 "EHLO outprodmail01.cc.columbia.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753001AbdA0BGZ (ORCPT ); Thu, 26 Jan 2017 20:06:25 -0500 Received: from hazelnut (hazelnut.cc.columbia.edu [128.59.213.250]) by outprodmail01.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id v0R156Kg051166 for ; Thu, 26 Jan 2017 20:05:09 -0500 Received: from hazelnut (localhost.localdomain [127.0.0.1]) by hazelnut (Postfix) with ESMTP id A495A80 for ; Thu, 26 Jan 2017 20:05:09 -0500 (EST) Received: from sendprodmail01.cc.columbia.edu (sendprodmail01.cc.columbia.edu [128.59.72.13]) by hazelnut (Postfix) with ESMTP id 7B81880 for ; Thu, 26 Jan 2017 20:05:09 -0500 (EST) Received: from mail-qt0-f197.google.com (mail-qt0-f197.google.com [209.85.216.197]) by sendprodmail01.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id v0R159EP062524 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 26 Jan 2017 20:05:09 -0500 Received: by mail-qt0-f197.google.com with SMTP id x49so254844206qtc.7 for ; Thu, 26 Jan 2017 17:05:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=2uJNgb9mopyGXoXQqskpU3FnuAJeeMEAGflQt+lHLYY=; b=FrN+Q3we7Bj3q+bg7Efbs8GeiV/md3T09eN8TDJ0y/J97cFUAdStKMtZxeU5HjjI41 9yhVVbq1xZ+imEDxdy+tNVJSgcNTJmKXW7FbOaxLfvmeJ2is21+T6z8e6FXUa4Yy+22v GVATb7t37d46uzRN4wLSSdWxlHGzrnZG1yiWe3kgVI6+iGLwMgWTr6rYaoxLouhgRCPK 0+9Dg89GJbfg7BvrLBvtsyGiTD/T18dMY0ktdP90SMHnCww8lx5oQhATGaJM5JqRdhZK ctV3BxJYKvrDjXKofBPRW0f1FCCoccF92LdR5tDFUHiBjpAtQyRc4DmS7IQyvHFNWzbk ddDw== X-Gm-Message-State: AIkVDXJXFP+tmiqMmJgIjCO3FV14aZwgu5GQ4rypsko2EPfOPm5x7wos08+vgw2wvLwkfdyugZLFP4mjMIdOYBHZhAQdm38nevpkZfdUvFHGpNO/wHvrrwdLsOuab3U9VPROO5UkuIY1GJg= X-Received: by 10.55.197.194 with SMTP id k63mr5596321qkl.85.1485479109034; Thu, 26 Jan 2017 17:05:09 -0800 (PST) X-Received: by 10.55.197.194 with SMTP id k63mr5596301qkl.85.1485479108781; Thu, 26 Jan 2017 17:05:08 -0800 (PST) Received: from jintack.cs.columbia.edu ([2001:18d8:ffff:16:21a:4aff:feaa:f900]) by smtp.gmail.com with ESMTPSA id f68sm2709538qkf.34.2017.01.26.17.05.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Jan 2017 17:05:08 -0800 (PST) From: Jintack Lim To: pbonzini@redhat.com, rkrcmar@redhat.com, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux@armlinux.org.uk, catalin.marinas@arm.com, will.deacon@arm.com, andre.przywara@arm.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Subject: [RFC v2 07/10] KVM: arm/arm64: Set a background timer to the earliest timer expiration Date: Thu, 26 Jan 2017 20:04:57 -0500 Message-Id: <1485479100-4966-8-git-send-email-jintack@cs.columbia.edu> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1485479100-4966-1-git-send-email-jintack@cs.columbia.edu> References: <1485479100-4966-1-git-send-email-jintack@cs.columbia.edu> X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.78 on 128.59.72.13 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When scheduling a background timer, consider both of the virtual and physical timer and pick the earliest expiration time. Signed-off-by: Jintack Lim --- arch/arm/kvm/arm.c | 3 ++- virt/kvm/arm/arch_timer.c | 55 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index 9a34a3c..9e94f87 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -301,7 +301,8 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) { - return kvm_timer_should_fire(vcpu, vcpu_vtimer(vcpu)); + return kvm_timer_should_fire(vcpu, vcpu_vtimer(vcpu)) || + kvm_timer_should_fire(vcpu, vcpu_ptimer(vcpu)); } void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c index 3b6bd50..d3925e2 100644 --- a/virt/kvm/arm/arch_timer.c +++ b/virt/kvm/arm/arch_timer.c @@ -119,6 +119,35 @@ static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu, return 0; } +static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx) +{ + return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) && + (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE); +} + +/* + * Returns the earliest expiration time in ns among guest timers. + * Note that it will return 0 if none of timers can fire. + */ +static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu) +{ + u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX; + struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); + struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); + + if (kvm_timer_irq_can_fire(vtimer)) + min_virt = kvm_timer_compute_delta(vcpu, vtimer); + + if (kvm_timer_irq_can_fire(ptimer)) + min_phys = kvm_timer_compute_delta(vcpu, ptimer); + + /* If none of timers can fire, then return 0 */ + if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX)) + return 0; + + return min(min_virt, min_phys); +} + static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt) { struct arch_timer_cpu *timer; @@ -133,7 +162,7 @@ static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt) * PoV (NTP on the host may have forced it to expire * early). If we should have slept longer, restart it. */ - ns = kvm_timer_compute_delta(vcpu, vcpu_vtimer(vcpu)); + ns = kvm_timer_earliest_exp(vcpu); if (unlikely(ns)) { hrtimer_forward_now(hrt, ns_to_ktime(ns)); return HRTIMER_RESTART; @@ -143,12 +172,6 @@ static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt) return HRTIMER_NORESTART; } -static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx) -{ - return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) && - (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE); -} - bool kvm_timer_should_fire(struct kvm_vcpu *vcpu, struct arch_timer_context *timer_ctx) { @@ -233,26 +256,32 @@ void kvm_timer_schedule(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); + struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); BUG_ON(timer_is_armed(timer)); /* - * No need to schedule a background timer if the guest timer has + * No need to schedule a background timer if any guest timer has * already expired, because kvm_vcpu_block will return before putting * the thread to sleep. */ - if (kvm_timer_should_fire(vcpu, vtimer)) + if (kvm_timer_should_fire(vcpu, vtimer) || + kvm_timer_should_fire(vcpu, ptimer)) return; /* - * If the timer is not capable of raising interrupts (disabled or + * If both timers are not capable of raising interrupts (disabled or * masked), then there's no more work for us to do. */ - if (!kvm_timer_irq_can_fire(vtimer)) + if (!kvm_timer_irq_can_fire(vtimer) && + !kvm_timer_irq_can_fire(ptimer)) return; - /* The timer has not yet expired, schedule a background timer */ - timer_arm(timer, kvm_timer_compute_delta(vcpu, vtimer)); + /* + * The guest timers have not yet expired, schedule a background timer. + * Set the earliest expiration time among the guest timers. + */ + timer_arm(timer, kvm_timer_earliest_exp(vcpu)); } void kvm_timer_unschedule(struct kvm_vcpu *vcpu)