Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1571840

[RFC v3 07/10] KVM: arm/arm64: Set a background timer to the earliest timer expiration

From Jintack Lim <jintack@cs.columbia.edu>
Newsgroups linux.kernel
Subject [RFC v3 07/10] KVM: arm/arm64: Set a background timer to the earliest timer expiration
Date 2017-02-01 18:50 +0100
Message-ID <t67cC-tm-15@gated-at.bofh.it> (permalink)
References <t67cB-tm-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


When scheduling a background timer, consider both of the virtual and
physical timer and pick the earliest expiration time.

Signed-off-by: Jintack Lim <jintack@cs.columbia.edu>
---
 arch/arm/kvm/arm.c        |  3 ++-
 virt/kvm/arm/arch_timer.c | 53 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
index 0ecd6cf..21c493a 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -300,7 +300,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_vtimer(vcpu));
+	return kvm_timer_should_fire(vcpu_vtimer(vcpu)) ||
+	       kvm_timer_should_fire(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 1b086fd6..89bdb79 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -118,6 +118,35 @@ static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
 	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(vtimer);
+
+	if (kvm_timer_irq_can_fire(ptimer))
+		min_phys = kvm_timer_compute_delta(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;
@@ -132,7 +161,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_vtimer(vcpu));
+	ns = kvm_timer_earliest_exp(vcpu);
 	if (unlikely(ns)) {
 		hrtimer_forward_now(hrt, ns_to_ktime(ns));
 		return HRTIMER_RESTART;
@@ -142,12 +171,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 arch_timer_context *timer_ctx)
 {
 	u64 cval, now;
@@ -215,26 +238,30 @@ 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(vtimer))
+	if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(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(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)
-- 
1.9.1

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[RFC v3 00/10] Provide the EL1 physical timer to the VM Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
  [RFC v3 04/10] KVM: arm/arm64: Add the EL1 physical timer context Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
    Re: [RFC v3 04/10] KVM: arm/arm64: Add the EL1 physical timer context Christoffer Dall <cdall@linaro.org> - 2017-02-02 11:20 +0100
  [RFC v3 08/10] KVM: arm/arm64: Set up a background timer for the physical timer emulation Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
    Re: [RFC v3 08/10] KVM: arm/arm64: Set up a background timer for the  physical timer emulation Christoffer Dall <cdall@linaro.org> - 2017-02-02 11:40 +0100
  [RFC v3 07/10] KVM: arm/arm64: Set a background timer to the earliest timer expiration Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
    Re: [RFC v3 07/10] KVM: arm/arm64: Set a background timer to the  earliest timer expiration Christoffer Dall <cdall@linaro.org> - 2017-02-02 11:40 +0100
  [RFC v3 03/10] KVM: arm/arm64: Decouple kvm timer functions from virtual timer Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
    Re: [RFC v3 03/10] KVM: arm/arm64: Decouple kvm timer functions from  virtual timer Christoffer Dall <cdall@linaro.org> - 2017-02-02 11:10 +0100
  [RFC v3 02/10] KVM: arm/arm64: Move cntvoff to each timer context Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
    Re: [RFC v3 02/10] KVM: arm/arm64: Move cntvoff to each timer context Christoffer Dall <cdall@linaro.org> - 2017-02-02 11:10 +0100
  [RFC v3 01/10] KVM: arm/arm64: Abstract virtual timer context into separate structure Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
  [RFC v3 06/10] KVM: arm/arm64: Update the physical timer interrupt level Jintack Lim <jintack@cs.columbia.edu> - 2017-02-01 18:50 +0100
    Re: [RFC v3 06/10] KVM: arm/arm64: Update the physical timer  interrupt level Christoffer Dall <cdall@linaro.org> - 2017-02-02 11:20 +0100
  Re: [RFC v3 00/10] Provide the EL1 physical timer to the VM Christoffer Dall <cdall@linaro.org> - 2017-02-02 13:40 +0100
    Re: [RFC v3 00/10] Provide the EL1 physical timer to the VM Jintack Lim <jintack@cs.columbia.edu> - 2017-02-02 16:00 +0100
      Re: [RFC v3 00/10] Provide the EL1 physical timer to the VM Christoffer Dall <cdall@linaro.org> - 2017-02-02 16:10 +0100
      Re: [RFC v3 00/10] Provide the EL1 physical timer to the VM Christoffer Dall <cdall@linaro.org> - 2017-02-03 13:40 +0100
        Re: [RFC v3 00/10] Provide the EL1 physical timer to the VM Jintack Lim <jintack@cs.columbia.edu> - 2017-02-03 14:20 +0100
          Re: [RFC v3 00/10] Provide the EL1 physical timer to the VM Christoffer Dall <cdall@linaro.org> - 2017-02-03 14:40 +0100

csiph-web