Skip to content

Commit f2a2129

Browse files
Christoffer Dallchazy
authored andcommitted
KVM: arm/arm64: Use separate timer for phys timer emulation
We were using the same hrtimer for emulating the physical timer and for making sure a blocking VCPU thread would be eventually woken up. That worked fine in the previous arch timer design, but as we are about to actually use the soft timer expire function for the physical timer emulation, change the logic to use a dedicated hrtimer. This has the added benefit of not having to cancel any work in the sync path, which in turn allows us to run the flush and sync with IRQs disabled. Note that the hrtimer used to program the host kernel's timer to generate an exit from the guest when the emulated physical timer fires never has to inject any work, and to share the soft_timer_cancel() function with the bg_timer, we change the function to only cancel any pending work if the pointer to the work struct is not null. Acked-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Christoffer Dall <cdall@linaro.org>
1 parent ee9bb9a commit f2a2129

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

include/kvm/arm_arch_timer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ struct arch_timer_cpu {
4848
/* Work queued with the above timer expires */
4949
struct work_struct expired;
5050

51+
/* Physical timer emulation */
52+
struct hrtimer phys_timer;
53+
5154
/* Is the timer enabled */
5255
bool enabled;
5356
};

virt/kvm/arm/arch_timer.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ static void soft_timer_start(struct hrtimer *hrt, u64 ns)
6565
static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work)
6666
{
6767
hrtimer_cancel(hrt);
68-
cancel_work_sync(work);
68+
if (work)
69+
cancel_work_sync(work);
6970
}
7071

7172
static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
@@ -172,6 +173,12 @@ static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
172173
return HRTIMER_NORESTART;
173174
}
174175

176+
static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
177+
{
178+
WARN(1, "Timer only used to ensure guest exit - unexpected event.");
179+
return HRTIMER_NORESTART;
180+
}
181+
175182
bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
176183
{
177184
u64 cval, now;
@@ -249,7 +256,7 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
249256
}
250257

251258
/* Schedule the background timer for the emulated timer. */
252-
static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
259+
static void phys_timer_emulate(struct kvm_vcpu *vcpu,
253260
struct arch_timer_context *timer_ctx)
254261
{
255262
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
@@ -261,7 +268,7 @@ static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
261268
return;
262269

263270
/* The timer has not yet expired, schedule a background timer */
264-
soft_timer_start(&timer->bg_timer, kvm_timer_compute_delta(timer_ctx));
271+
soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(timer_ctx));
265272
}
266273

267274
/*
@@ -414,7 +421,7 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
414421
kvm_timer_update_state(vcpu);
415422

416423
/* Set the background timer for the physical timer emulation. */
417-
kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu));
424+
phys_timer_emulate(vcpu, vcpu_ptimer(vcpu));
418425

419426
if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
420427
kvm_timer_flush_hwstate_user(vcpu);
@@ -437,7 +444,7 @@ void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
437444
* This is to cancel the background timer for the physical timer
438445
* emulation if it is set.
439446
*/
440-
soft_timer_cancel(&timer->bg_timer, &timer->expired);
447+
soft_timer_cancel(&timer->phys_timer, NULL);
441448

442449
/*
443450
* The guest could have modified the timer registers or the timer
@@ -497,6 +504,9 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
497504
hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
498505
timer->bg_timer.function = kvm_bg_timer_expire;
499506

507+
hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
508+
timer->phys_timer.function = kvm_phys_timer_expire;
509+
500510
vtimer->irq.irq = default_vtimer_irq.irq;
501511
ptimer->irq.irq = default_ptimer_irq.irq;
502512
}
@@ -605,6 +615,7 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
605615
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
606616

607617
soft_timer_cancel(&timer->bg_timer, &timer->expired);
618+
soft_timer_cancel(&timer->phys_timer, NULL);
608619
kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
609620
}
610621

0 commit comments

Comments
 (0)