Skip to content

Commit f3b138c

Browse files
Andrey Smetaninbonzini
authored andcommitted
kvm/x86: Update SynIC timers on guest entry only
Consolidate updating the Hyper-V SynIC timers in a single place: on guest entry in processing KVM_REQ_HV_STIMER request. This simplifies the overall logic, and makes sure the most current state of msrs and guest clock is used for arming the timers (to achieve that, KVM_REQ_HV_STIMER has to be processed after KVM_REQ_CLOCK_UPDATE). Signed-off-by: Andrey Smetanin <asmetanin@virtuozzo.com> Reviewed-by: Roman Kagan <rkagan@virtuozzo.com> CC: Gleb Natapov <gleb@kernel.org> CC: Paolo Bonzini <pbonzini@redhat.com> CC: Roman Kagan <rkagan@virtuozzo.com> CC: Denis V. Lunev <den@openvz.org> CC: qemu-devel@nongnu.org Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 7be58a6 commit f3b138c

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

arch/x86/kvm/hyperv.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static u64 get_time_ref_counter(struct kvm *kvm)
389389
return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
390390
}
391391

392-
static void stimer_mark_expired(struct kvm_vcpu_hv_stimer *stimer,
392+
static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
393393
bool vcpu_kick)
394394
{
395395
struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
@@ -417,7 +417,7 @@ static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
417417
struct kvm_vcpu_hv_stimer *stimer;
418418

419419
stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
420-
stimer_mark_expired(stimer, true);
420+
stimer_mark_pending(stimer, true);
421421

422422
return HRTIMER_NORESTART;
423423
}
@@ -462,7 +462,7 @@ static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
462462
* "If a one shot is enabled and the specified count is in
463463
* the past, it will expire immediately."
464464
*/
465-
stimer_mark_expired(stimer, false);
465+
stimer_mark_pending(stimer, false);
466466
return 0;
467467
}
468468

@@ -475,30 +475,24 @@ static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
475475
static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
476476
bool host)
477477
{
478+
stimer_cleanup(stimer);
478479
if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
479480
config &= ~HV_STIMER_ENABLE;
480481
stimer->config = config;
481-
stimer_cleanup(stimer);
482-
if (stimer->config & HV_STIMER_ENABLE)
483-
if (stimer_start(stimer))
484-
return 1;
482+
stimer_mark_pending(stimer, false);
485483
return 0;
486484
}
487485

488486
static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
489487
bool host)
490488
{
491-
stimer->count = count;
492-
493489
stimer_cleanup(stimer);
490+
stimer->count = count;
494491
if (stimer->count == 0)
495492
stimer->config &= ~HV_STIMER_ENABLE;
496-
else if (stimer->config & HV_STIMER_AUTOENABLE) {
493+
else if (stimer->config & HV_STIMER_AUTOENABLE)
497494
stimer->config |= HV_STIMER_ENABLE;
498-
if (stimer_start(stimer))
499-
return 1;
500-
}
501-
495+
stimer_mark_pending(stimer, false);
502496
return 0;
503497
}
504498

@@ -582,18 +576,24 @@ void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
582576
{
583577
struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
584578
struct kvm_vcpu_hv_stimer *stimer;
585-
u64 time_now;
579+
u64 time_now, exp_time;
586580
int i;
587581

588582
for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
589583
if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
590584
stimer = &hv_vcpu->stimer[i];
591585
if (stimer->config & HV_STIMER_ENABLE) {
592-
time_now = get_time_ref_counter(vcpu->kvm);
593-
if (time_now >= stimer->exp_time)
594-
stimer_expiration(stimer);
586+
exp_time = stimer->exp_time;
587+
588+
if (exp_time) {
589+
time_now =
590+
get_time_ref_counter(vcpu->kvm);
591+
if (time_now >= exp_time)
592+
stimer_expiration(stimer);
593+
}
595594

596-
if (stimer->config & HV_STIMER_ENABLE)
595+
if ((stimer->config & HV_STIMER_ENABLE) &&
596+
stimer->count)
597597
stimer_start(stimer);
598598
else
599599
stimer_cleanup(stimer);

arch/x86/kvm/x86.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6508,6 +6508,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
65086508
r = 0;
65096509
goto out;
65106510
}
6511+
6512+
/*
6513+
* KVM_REQ_HV_STIMER has to be processed after
6514+
* KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
6515+
* depend on the guest clock being up-to-date
6516+
*/
65116517
if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
65126518
kvm_hv_process_stimers(vcpu);
65136519
}

0 commit comments

Comments
 (0)