Skip to content

Commit 1c5631c

Browse files
Marc Zyngierchazy
authored andcommitted
KVM: arm/arm64: Handle forward time correction gracefully
On a host that runs NTP, corrections can have a direct impact on the background timer that we program on the behalf of a vcpu. In particular, NTP performing a forward correction will result in a timer expiring sooner than expected from a guest point of view. Not a big deal, we kick the vcpu anyway. But on wake-up, the vcpu thread is going to perform a check to find out whether or not it should block. And at that point, the timer check is going to say "timer has not expired yet, go back to sleep". This results in the timer event being lost forever. There are multiple ways to handle this. One would be record that the timer has expired and let kvm_cpu_has_pending_timer return true in that case, but that would be fairly invasive. Another is to check for the "short sleep" condition in the hrtimer callback, and restart the timer for the remaining time when the condition is detected. This patch implements the latter, with a bit of refactoring in order to avoid too much code duplication. Cc: <stable@vger.kernel.org> Reported-by: Alexander Graf <agraf@suse.de> Reviewed-by: Alexander Graf <agraf@suse.de> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
1 parent 7d4bd1d commit 1c5631c

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

virt/kvm/arm/arch_timer.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,55 @@ static void kvm_timer_inject_irq_work(struct work_struct *work)
9191
vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
9292
vcpu->arch.timer_cpu.armed = false;
9393

94+
WARN_ON(!kvm_timer_should_fire(vcpu));
95+
9496
/*
9597
* If the vcpu is blocked we want to wake it up so that it will see
9698
* the timer has expired when entering the guest.
9799
*/
98100
kvm_vcpu_kick(vcpu);
99101
}
100102

103+
static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu)
104+
{
105+
cycle_t cval, now;
106+
107+
cval = vcpu->arch.timer_cpu.cntv_cval;
108+
now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
109+
110+
if (now < cval) {
111+
u64 ns;
112+
113+
ns = cyclecounter_cyc2ns(timecounter->cc,
114+
cval - now,
115+
timecounter->mask,
116+
&timecounter->frac);
117+
return ns;
118+
}
119+
120+
return 0;
121+
}
122+
101123
static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
102124
{
103125
struct arch_timer_cpu *timer;
126+
struct kvm_vcpu *vcpu;
127+
u64 ns;
128+
104129
timer = container_of(hrt, struct arch_timer_cpu, timer);
130+
vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
131+
132+
/*
133+
* Check that the timer has really expired from the guest's
134+
* PoV (NTP on the host may have forced it to expire
135+
* early). If we should have slept longer, restart it.
136+
*/
137+
ns = kvm_timer_compute_delta(vcpu);
138+
if (unlikely(ns)) {
139+
hrtimer_forward_now(hrt, ns_to_ktime(ns));
140+
return HRTIMER_RESTART;
141+
}
142+
105143
queue_work(wqueue, &timer->expired);
106144
return HRTIMER_NORESTART;
107145
}
@@ -176,8 +214,6 @@ static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
176214
void kvm_timer_schedule(struct kvm_vcpu *vcpu)
177215
{
178216
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
179-
u64 ns;
180-
cycle_t cval, now;
181217

182218
BUG_ON(timer_is_armed(timer));
183219

@@ -197,14 +233,7 @@ void kvm_timer_schedule(struct kvm_vcpu *vcpu)
197233
return;
198234

199235
/* The timer has not yet expired, schedule a background timer */
200-
cval = timer->cntv_cval;
201-
now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
202-
203-
ns = cyclecounter_cyc2ns(timecounter->cc,
204-
cval - now,
205-
timecounter->mask,
206-
&timecounter->frac);
207-
timer_arm(timer, ns);
236+
timer_arm(timer, kvm_timer_compute_delta(vcpu));
208237
}
209238

210239
void kvm_timer_unschedule(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)