Skip to content

Commit bbdd52c

Browse files
Christoffer Dallchazy
authored andcommitted
KVM: arm/arm64: Avoid phys timer emulation in vcpu entry/exit
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 <cdall@linaro.org> Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent cda93b7 commit bbdd52c

1 file changed

Lines changed: 51 additions & 24 deletions

File tree

‎virt/kvm/arm/arch_timer.c‎

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,27 @@ static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
202202

203203
static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
204204
{
205-
WARN(1, "Timer only used to ensure guest exit - unexpected event.");
205+
struct arch_timer_context *ptimer;
206+
struct arch_timer_cpu *timer;
207+
struct kvm_vcpu *vcpu;
208+
u64 ns;
209+
210+
timer = container_of(hrt, struct arch_timer_cpu, phys_timer);
211+
vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
212+
ptimer = vcpu_ptimer(vcpu);
213+
214+
/*
215+
* Check that the timer has really expired from the guest's
216+
* PoV (NTP on the host may have forced it to expire
217+
* early). If not ready, schedule for a later time.
218+
*/
219+
ns = kvm_timer_compute_delta(ptimer);
220+
if (unlikely(ns)) {
221+
hrtimer_forward_now(hrt, ns_to_ktime(ns));
222+
return HRTIMER_RESTART;
223+
}
224+
225+
kvm_timer_update_irq(vcpu, true, ptimer);
206226
return HRTIMER_NORESTART;
207227
}
208228

@@ -256,24 +276,28 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
256276
}
257277

258278
/* Schedule the background timer for the emulated timer. */
259-
static void phys_timer_emulate(struct kvm_vcpu *vcpu,
260-
struct arch_timer_context *timer_ctx)
279+
static void phys_timer_emulate(struct kvm_vcpu *vcpu)
261280
{
262281
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
282+
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
263283

264-
if (kvm_timer_should_fire(timer_ctx))
265-
return;
266-
267-
if (!kvm_timer_irq_can_fire(timer_ctx))
284+
/*
285+
* If the timer can fire now we have just raised the IRQ line and we
286+
* don't need to have a soft timer scheduled for the future. If the
287+
* timer cannot fire at all, then we also don't need a soft timer.
288+
*/
289+
if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) {
290+
soft_timer_cancel(&timer->phys_timer, NULL);
268291
return;
292+
}
269293

270-
/* The timer has not yet expired, schedule a background timer */
271-
soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(timer_ctx));
294+
soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer));
272295
}
273296

274297
/*
275-
* Check if there was a change in the timer state (should we raise or lower
276-
* the line level to the GIC).
298+
* Check if there was a change in the timer state, so that we should either
299+
* raise or lower the line level to the GIC or schedule a background timer to
300+
* emulate the physical timer.
277301
*/
278302
static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
279303
{
@@ -295,6 +319,8 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
295319

296320
if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
297321
kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
322+
323+
phys_timer_emulate(vcpu);
298324
}
299325

300326
static void vtimer_save_state(struct kvm_vcpu *vcpu)
@@ -441,6 +467,9 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
441467

442468
if (has_vhe())
443469
disable_el1_phys_timer_access();
470+
471+
/* Set the background timer for the physical timer emulation. */
472+
phys_timer_emulate(vcpu);
444473
}
445474

446475
bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
@@ -476,12 +505,6 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
476505

477506
if (unlikely(!timer->enabled))
478507
return;
479-
480-
if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
481-
kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
482-
483-
/* Set the background timer for the physical timer emulation. */
484-
phys_timer_emulate(vcpu, vcpu_ptimer(vcpu));
485508
}
486509

487510
void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
@@ -496,6 +519,17 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
496519

497520
vtimer_save_state(vcpu);
498521

522+
/*
523+
* Cancel the physical timer emulation, because the only case where we
524+
* need it after a vcpu_put is in the context of a sleeping VCPU, and
525+
* in that case we already factor in the deadline for the physical
526+
* timer when scheduling the bg_timer.
527+
*
528+
* In any case, we re-schedule the hrtimer for the physical timer when
529+
* coming back to the VCPU thread in kvm_timer_vcpu_load().
530+
*/
531+
soft_timer_cancel(&timer->phys_timer, NULL);
532+
499533
/*
500534
* The kernel may decide to run userspace after calling vcpu_put, so
501535
* we reset cntvoff to 0 to ensure a consistent read between user
@@ -538,15 +572,8 @@ static void unmask_vtimer_irq(struct kvm_vcpu *vcpu)
538572
*/
539573
void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
540574
{
541-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
542575
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
543576

544-
/*
545-
* This is to cancel the background timer for the physical timer
546-
* emulation if it is set.
547-
*/
548-
soft_timer_cancel(&timer->phys_timer, NULL);
549-
550577
/*
551578
* If we entered the guest with the vtimer output asserted we have to
552579
* check if the guest has modified the timer so that we should lower

0 commit comments

Comments
 (0)