Skip to content

Commit f808495

Browse files
Andrey Smetaninbonzini
authored andcommitted
kvm/x86: Hyper-V unify stimer_start() and stimer_restart()
This will be used in future to start Hyper-V SynIC timer in several places by one logic in one function. Changes v2: * drop stimer->count == 0 check inside stimer_start() * comment stimer_start() assumptions 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 019b978 commit f808495

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

arch/x86/kvm/hyperv.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
408408
clear_bit(stimer->index,
409409
vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
410410
stimer->msg_pending = false;
411+
stimer->exp_time = 0;
411412
}
412413

413414
static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
@@ -420,24 +421,11 @@ static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
420421
return HRTIMER_NORESTART;
421422
}
422423

423-
static void stimer_restart(struct kvm_vcpu_hv_stimer *stimer)
424-
{
425-
u64 time_now;
426-
ktime_t ktime_now;
427-
u64 remainder;
428-
429-
time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
430-
ktime_now = ktime_get();
431-
432-
div64_u64_rem(time_now - stimer->exp_time, stimer->count, &remainder);
433-
stimer->exp_time = time_now + (stimer->count - remainder);
434-
435-
hrtimer_start(&stimer->timer,
436-
ktime_add_ns(ktime_now,
437-
100 * (stimer->exp_time - time_now)),
438-
HRTIMER_MODE_ABS);
439-
}
440-
424+
/*
425+
* stimer_start() assumptions:
426+
* a) stimer->count is not equal to 0
427+
* b) stimer->config has HV_STIMER_ENABLE flag
428+
*/
441429
static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
442430
{
443431
u64 time_now;
@@ -447,12 +435,21 @@ static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
447435
ktime_now = ktime_get();
448436

449437
if (stimer->config & HV_STIMER_PERIODIC) {
450-
if (stimer->count == 0)
451-
return -EINVAL;
438+
if (stimer->exp_time) {
439+
if (time_now >= stimer->exp_time) {
440+
u64 remainder;
441+
442+
div64_u64_rem(time_now - stimer->exp_time,
443+
stimer->count, &remainder);
444+
stimer->exp_time =
445+
time_now + (stimer->count - remainder);
446+
}
447+
} else
448+
stimer->exp_time = time_now + stimer->count;
452449

453-
stimer->exp_time = time_now + stimer->count;
454450
hrtimer_start(&stimer->timer,
455-
ktime_add_ns(ktime_now, 100 * stimer->count),
451+
ktime_add_ns(ktime_now,
452+
100 * (stimer->exp_time - time_now)),
456453
HRTIMER_MODE_ABS);
457454
return 0;
458455
}
@@ -580,7 +577,7 @@ static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
580577
if (!(stimer->config & HV_STIMER_PERIODIC))
581578
stimer->config &= ~HV_STIMER_ENABLE;
582579
else
583-
stimer_restart(stimer);
580+
stimer_start(stimer);
584581
}
585582

586583
void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)